diff --git a/assets/script/game/common/SingletonModuleComp.ts b/assets/script/game/common/SingletonModuleComp.ts index adbcd394..1c85e897 100644 --- a/assets/script/game/common/SingletonModuleComp.ts +++ b/assets/script/game/common/SingletonModuleComp.ts @@ -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); diff --git a/assets/script/game/initialize/Initialize.ts b/assets/script/game/initialize/Initialize.ts index 85e69a9c..ebcf8728 100644 --- a/assets/script/game/initialize/Initialize.ts +++ b/assets/script/game/initialize/Initialize.ts @@ -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); }); } diff --git a/assets/script/game/map/TalentItemComp.ts b/assets/script/game/map/TalentItemComp.ts index f432d3e0..2e9f4d90 100644 --- a/assets/script/game/map/TalentItemComp.ts +++ b/assets/script/game/map/TalentItemComp.ts @@ -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; diff --git a/assets/script/game/map/TalentsComp.ts b/assets/script/game/map/TalentsComp.ts index ee255808..770e347c 100644 --- a/assets/script/game/map/TalentsComp.ts +++ b/assets/script/game/map/TalentsComp.ts @@ -18,7 +18,7 @@ * - ScoreWeights(ScoreSet)—— 得分权重配置 * - 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(); - }); - } - }); } /** 刷新整体界面 */