perf(talent): 优化天赋系统的图集预加载与缓存逻辑

将天赋图集预加载从天赋界面初始化时提前至游戏公共资源加载阶段,新增全局单例缓存机制替换组件自身的静态缓存,移除冗余的异步加载代码与未使用的导入语句,修正TalentItemComp的初始默认天赋类型为Attack
This commit is contained in:
walkpan
2026-05-11 16:08:20 +08:00
parent 336d7d03db
commit bcef8fbc64
4 changed files with 26 additions and 25 deletions

View File

@@ -1,4 +1,4 @@
import { sys } from "cc";
import { sys, resources, SpriteAtlas } from "cc";
import { VM } from "../../../../extensions/oops-plugin-framework/assets/libs/model-view/ViewModel";
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { Initialize } from "../initialize/Initialize";
@@ -39,6 +39,10 @@ export class SingletonModuleComp extends ecs.Comp {
initialize: Initialize = null!;
/** 游戏地图 */
map: GameMap = null!;
/** 全局缓存的通用图集 */
uiconsAtlas: SpriteAtlas | null = null;
openid:string=''
mission:any={
status:0, //0:未开始 1:进行中 2:胜利 3:失败
@@ -282,6 +286,19 @@ export class SingletonModuleComp extends ecs.Comp {
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
return true
}
/**
* 在游戏载入早期调用,预加载常用图集
*/
preloadCommonAssets() {
resources.load("gui/uicons", SpriteAtlas, (err, atlas) => {
if (!err && atlas) {
this.uiconsAtlas = atlas;
} else {
mLogger.error(this.debugMode, 'SMC', "预加载 gui/uicons 图集失败:", err);
}
});
}
}
export var smc: SingletonModuleComp = ecs.getSingleton(SingletonModuleComp);

View File

@@ -83,6 +83,8 @@ export class Initialize extends ecs.Entity {
/** 加载公共资源(必备) */
private loadCommon(queue: AsyncQueue) {
queue.push((next: NextFunction, params: any, args: any) => {
// 顺便在这里预加载 SMC 需要的公共图集
smc.preloadCommonAssets();
oops.res.loadDir("common", next);
});
}

View File

@@ -38,15 +38,9 @@ export class TalentItemComp extends CCComp {
@property({ type: Node, tooltip: "图标背景" })
icon_bg: Node = null!;
private _talentId: TalentType = TalentType.DeadTrigger;
private _talentId: TalentType = TalentType.Attack;
private _onClickCallback: ((talentId: TalentType, currentLevel: number) => void) | null = null;
private _currentLevel: number = 0;
private static _cachedAtlas: SpriteAtlas | null = null;
public static setAtlas(atlas: SpriteAtlas) {
TalentItemComp._cachedAtlas = atlas;
}
private _talentInfo: TalentInfo | null = null;
@@ -97,12 +91,12 @@ export class TalentItemComp extends CCComp {
}
}
/** 单独更新图标,供父节点加载完图集后回调 */
/** 单独更新图标,供父节点加载完图集后回调或自身更新时调用 */
public refreshIcon() {
if (!this._talentInfo || !this.icon_node || !this._talentInfo.icon) return;
if (TalentItemComp._cachedAtlas) {
const frame = TalentItemComp._cachedAtlas.getSpriteFrame(this._talentInfo.icon);
if (smc.uiconsAtlas) {
const frame = smc.uiconsAtlas.getSpriteFrame(this._talentInfo.icon);
if (frame && this.icon_node.isValid) {
const sprite = this.icon_node.getComponent(Sprite) || this.icon_node.addComponent(Sprite);
sprite.spriteFrame = frame;

View File

@@ -18,7 +18,7 @@
* - ScoreWeightsScoreSet—— 得分权重配置
* - GameEvent.MissionEnd / MissionStart —— 游戏生命周期事件
*/
import { _decorator, Node, Label, Button, ProgressBar, instantiate, Prefab, resources, SpriteAtlas } from "cc";
import { _decorator, Node, Label, Button, ProgressBar, instantiate, Prefab } from "cc";
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
import { mLogger } from "../common/Logger";
@@ -88,20 +88,8 @@ export class TalentsComp extends CCComp {
}
onAdded(args: any) {
// 先立刻刷新界面结构,保证面板秒开
// 直接刷新界面,因为图集已经在游戏启动时被 smc 预加载并缓存
this.refreshUI();
// 异步预加载图集,不阻塞主界面的生成
resources.load("gui/uicons", SpriteAtlas, (err, atlas) => {
if (!err && atlas) {
TalentItemComp.setAtlas(atlas);
// 图集加载完毕后,让所有的子组件只刷新一下自己的图标
this.talents_content.children.forEach(child => {
let comp = child.getComponent(TalentItemComp);
if (comp) comp.refreshIcon();
});
}
});
}
/** 刷新整体界面 */