【踩坑】CocosCreator Sprite 在移动端显示不出来

发布时间 2023-09-20 16:02:27作者: bakabird1998

如果某个 Sprite 组件的图片在 编辑器环境、预览模式(浏览器)下都能正常显示了。

但是在移动端硬是显示不出来。

先确认下是不是用了 继承了原有 Sprite 的自定义 Sprite 组件。

像这样:

export class LevelBgSprite extends Sprite {
      // anycode 
}

如果是的话....请确认下重写 start update onLoad 这些父类方法的时候有没有调对应的 super.xxx

比如:

public onLoad(): void {
     this._uiTran = this.getComponent(UITransform);
     Go.fuiSys.makeUIFullScreenButKeepOriginRatio(this._uiTran)
     this._whCache = v2(this._uiTran.width, this._uiTran.height);
 }
 
 public start(): void {
     this.getMaterialInstance(0).setProperty('texSize', this._whCache);
     this.getMaterialInstance(0).setProperty('gridSize', this.gridSize);
     this.getMaterialInstance(0).setProperty('rectSize', this.rectSize);
 }

就需要改成:

public onLoad(): void {
     super.onLoad();
     this._uiTran = this.getComponent(UITransform);
     Go.fuiSys.makeUIFullScreenButKeepOriginRatio(this._uiTran)
     this._whCache = v2(this._uiTran.width, this._uiTran.height);
 }
 
 public start(): void {
     super.start();
     this.getMaterialInstance(0).setProperty('texSize', this._whCache);
     this.getMaterialInstance(0).setProperty('gridSize', this.gridSize);
     this.getMaterialInstance(0).setProperty('rectSize', this.rectSize);
 }