fix: 修复图集GC回收和天赋图标加载异常问题
1. 为uicons图集增加引用计数防止被GC回收导致spriteFrames为空 2. 增加图集spriteFrames存在性校验,避免空引用报错 3. 将天赋图标配置转为字符串类型,防止纯数字配置导致获取失败
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user