diff --git a/assets/script/game/common/SingletonModuleComp.ts b/assets/script/game/common/SingletonModuleComp.ts index b5003c6c..11614536 100644 --- a/assets/script/game/common/SingletonModuleComp.ts +++ b/assets/script/game/common/SingletonModuleComp.ts @@ -292,6 +292,8 @@ export class SingletonModuleComp extends ecs.Comp { preloadCommonAssets() { resources.load("gui/uicons", SpriteAtlas, (err, atlas) => { if (!err && atlas) { + // 增加引用计数,防止图集被引擎自动垃圾回收(GC)导致底层 spriteFrames 为 null + atlas.addRef(); this.uiconsAtlas = atlas; } else { mLogger.error(this.debugMode, 'SMC', "预加载 gui/uicons 图集失败:", err); diff --git a/assets/script/game/map/TalentItemComp.ts b/assets/script/game/map/TalentItemComp.ts index 2e9f4d90..e9b0e7ba 100644 --- a/assets/script/game/map/TalentItemComp.ts +++ b/assets/script/game/map/TalentItemComp.ts @@ -95,8 +95,10 @@ export class TalentItemComp extends CCComp { public refreshIcon() { if (!this._talentInfo || !this.icon_node || !this._talentInfo.icon) return; - if (smc.uiconsAtlas) { - const frame = smc.uiconsAtlas.getSpriteFrame(this._talentInfo.icon); + if (smc.uiconsAtlas && smc.uiconsAtlas.spriteFrames) { + // 确保 icon 是字符串类型,防止配置写成纯数字导致底层 getSpriteFrame 报错 + const iconStr = String(this._talentInfo.icon); + const frame = smc.uiconsAtlas.getSpriteFrame(iconStr); if (frame && this.icon_node.isValid) { const sprite = this.icon_node.getComponent(Sprite) || this.icon_node.addComponent(Sprite); sprite.spriteFrame = frame;