1.0.0 的 Cocos3D没有WXSubContextView这个组件,我们该怎么实现呢?
1.首先我们要明白WXSubContextView这个组件的原理
onLoad () {
// Setup subcontext canvas size
if (wx.getOpenDataContext) {
this._context = wx.getOpenDataContext();
// reset sharedCanvas width and height
this.reset();
this._tex.setPremultiplyAlpha(true);
this._tex.initWithElement(sharedCanvas);
this._sprite = this.node.getComponent(cc.Sprite);
if (!this._sprite) {
this._sprite = this.node.addComponent(cc.Sprite);
this._sprite.srcBlendFactor = cc.macro.BlendFactor.ONE;
}
this._sprite.spriteFrame = new cc.SpriteFrame(this._tex);
}
else {
this.enabled = false;
}
}
我们查看源代码发现主要是使用initWithElement把sharedCanvas渲染到一个精灵上,
2 既然如此,那我们如法炮制就行了
首先我们要感谢贡献者 [size=13.006px]魏书 ,他贡献了用微信api实现的子域排行榜功能,代码只有十几k
可直接使用github地址
3.我们下载好需要修改相应的地方才能够使用,毕竟3d的api和2d差别还是挺大的
最重要的一点就是下面这一段
updateRankList() {
if(!window["wx"]) return;
if(!this.rankTexture) return;
let sharedCanvas = window["wx"].getOpenDataContext().canvas
this.rankTexture.image = new ImageAsset(sharedCanvas);
this.rankSpriteFrame.texture = this.rankTexture;
this.rankRender.spriteFrame = this.rankSpriteFrame;
}
找到这个方法,改成上面就行了
看下效果
希望对大家有所帮助。。。
全部评论