fix: 修复图集GC回收和天赋图标加载异常问题

1. 为uicons图集增加引用计数防止被GC回收导致spriteFrames为空
2. 增加图集spriteFrames存在性校验,避免空引用报错
3. 将天赋图标配置转为字符串类型,防止纯数字配置导致获取失败
This commit is contained in:
walkpan
2026-05-13 14:31:01 +08:00
parent 8e152eb710
commit bafeccbeef
2 changed files with 6 additions and 2 deletions

View File

@@ -292,6 +292,8 @@ export class SingletonModuleComp extends ecs.Comp {
preloadCommonAssets() { preloadCommonAssets() {
resources.load("gui/uicons", SpriteAtlas, (err, atlas) => { resources.load("gui/uicons", SpriteAtlas, (err, atlas) => {
if (!err && atlas) { if (!err && atlas) {
// 增加引用计数防止图集被引擎自动垃圾回收GC导致底层 spriteFrames 为 null
atlas.addRef();
this.uiconsAtlas = atlas; this.uiconsAtlas = atlas;
} else { } else {
mLogger.error(this.debugMode, 'SMC', "预加载 gui/uicons 图集失败:", err); mLogger.error(this.debugMode, 'SMC', "预加载 gui/uicons 图集失败:", err);

View File

@@ -95,8 +95,10 @@ export class TalentItemComp extends CCComp {
public refreshIcon() { public refreshIcon() {
if (!this._talentInfo || !this.icon_node || !this._talentInfo.icon) return; if (!this._talentInfo || !this.icon_node || !this._talentInfo.icon) return;
if (smc.uiconsAtlas) { if (smc.uiconsAtlas && smc.uiconsAtlas.spriteFrames) {
const frame = smc.uiconsAtlas.getSpriteFrame(this._talentInfo.icon); // 确保 icon 是字符串类型,防止配置写成纯数字导致底层 getSpriteFrame 报错
const iconStr = String(this._talentInfo.icon);
const frame = smc.uiconsAtlas.getSpriteFrame(iconStr);
if (frame && this.icon_node.isValid) { if (frame && this.icon_node.isValid) {
const sprite = this.icon_node.getComponent(Sprite) || this.icon_node.addComponent(Sprite); const sprite = this.icon_node.getComponent(Sprite) || this.icon_node.addComponent(Sprite);
sprite.spriteFrame = frame; sprite.spriteFrame = frame;