From bafeccbeef0f7372c33d0de04c3885cf86f79a84 Mon Sep 17 00:00:00 2001 From: walkpan Date: Wed, 13 May 2026 14:31:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=9B=BE=E9=9B=86GC?= =?UTF-8?q?=E5=9B=9E=E6=94=B6=E5=92=8C=E5=A4=A9=E8=B5=8B=E5=9B=BE=E6=A0=87?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 为uicons图集增加引用计数防止被GC回收导致spriteFrames为空 2. 增加图集spriteFrames存在性校验,避免空引用报错 3. 将天赋图标配置转为字符串类型,防止纯数字配置导致获取失败 --- assets/script/game/common/SingletonModuleComp.ts | 2 ++ assets/script/game/map/TalentItemComp.ts | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) 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;