diff --git a/assets/core/gui/Gui.ts b/assets/core/gui/Gui.ts index 4ddc09d..96f64cc 100644 --- a/assets/core/gui/Gui.ts +++ b/assets/core/gui/Gui.ts @@ -7,25 +7,37 @@ export namespace gui { /** 注册界面组件 */ export function register(key: string, config: UIConfig) { return function (ctor: any) { - ctor.oopsGuiKey = key; - setConfig(key, config); + //@ts-ignore + ctor[gui.internal.GUI_KEY] = key; + internal.setConfig(key, config); }; } - /** 获取界面组件配置 */ - export function getConfig(key: string) { - return configs[key]; - } + /** 框架内部使用方法 */ + export namespace internal { + /** 界面唯一标记变量名 */ + export const GUI_KEY = "OOPS_GUI_KEY"; + /** 获取界面组件配置 */ + export function getConfig(key: string) { + return configs[key]; + } - /** 获取界面组件配置 */ - export function setConfig(key: string, config: UIConfig) { - configs[key] = config; - } + /** 获取界面组件配置 */ + export function setConfig(key: string, config: UIConfig) { + let c = getConfig(key); + if (c == null) { + configs[key] = config; + } + else { + console.error(`界面${key}重复注册`); + } + } - /** 初始化界面组件配置 */ - export function initConfigs(uicm: UIConfigMap) { - for (const key in uicm) { - configs[key] = uicm[key]; + /** 初始化界面组件配置 */ + export function initConfigs(uicm: UIConfigMap) { + for (const key in uicm) { + configs[key] = uicm[key]; + } } } } \ No newline at end of file diff --git a/assets/core/gui/layer/LayerEnum.ts b/assets/core/gui/layer/LayerEnum.ts index 70d1ebb..9879f98 100644 --- a/assets/core/gui/layer/LayerEnum.ts +++ b/assets/core/gui/layer/LayerEnum.ts @@ -1,7 +1,7 @@ import { UIConfig } from "./UIConfig"; /** 界面编号 */ -export type Uiid = number | string | UIConfig; +export type Uiid = number | string | UIConfig | Function; /** 界面配置集合 */ export type UIConfigMap = { [key: string]: UIConfig } diff --git a/assets/core/gui/layer/LayerManager.ts b/assets/core/gui/layer/LayerManager.ts index a4e5184..aa4ea04 100644 --- a/assets/core/gui/layer/LayerManager.ts +++ b/assets/core/gui/layer/LayerManager.ts @@ -135,11 +135,9 @@ export class LayerManager { /** * 初始化所有UI的配置对象 * @param configs 配置对象 - * @deprecated 后续将界面配置通过界面视图组件注册,使用gui.register注册每一个界面 */ init(configs: UIConfigMap): void { - // this.configs = configs; - gui.initConfigs(configs); + gui.internal.initConfigs(configs); } /** @@ -177,19 +175,26 @@ export class LayerManager { let key = ""; let config: UIConfig = null!; - // 确定 key 和 config + // 界面配置 if (typeof uiid === 'object') { if (uiid.bundle == null) uiid.bundle = resLoader.defaultBundleName; key = uiid.bundle + "_" + uiid.prefab; - config = gui.getConfig(key); + config = gui.internal.getConfig(key); if (config == null) { config = uiid; - gui.setConfig(key, uiid); + gui.internal.setConfig(key, uiid); } } + // 界面对象 - 配合gui.register使用 + else if (uiid instanceof Function) { + //@ts-ignore + key = uiid[gui.internal.GUI_KEY]; + config = gui.internal.getConfig(key); + } + // 界面唯一标记 else { key = uiid.toString(); - config = gui.getConfig(key); + config = gui.internal.getConfig(key); if (config == null) { console.error(`打开编号为【${uiid}】的界面失败,配置信息不存在`); } @@ -276,7 +281,7 @@ export class LayerManager { if (comp && comp.params) { // 释放显示的界面 if (node.parent) { - let uiid = gui.getConfig(comp.params.uiid); + let uiid = gui.internal.getConfig(comp.params.uiid); this.remove(uiid, isDestroy); } // 释放缓存中的界面 diff --git a/assets/libs/ecs/ECS.ts b/assets/libs/ecs/ECS.ts index 65dacff..fbc2431 100644 --- a/assets/libs/ecs/ECS.ts +++ b/assets/libs/ecs/ECS.ts @@ -153,7 +153,7 @@ export namespace ecs { ECSModel.compAddOrRemove.set(ctor.tid, []); } else { - throw new Error(`重复注册组件: ${name}.`); + throw new Error(`ECS 组件重复注册: ${name}.`); } } } diff --git a/assets/module/common/CCComp.ts b/assets/module/common/CCComp.ts index 784c5c4..639307f 100644 --- a/assets/module/common/CCComp.ts +++ b/assets/module/common/CCComp.ts @@ -45,7 +45,12 @@ export abstract class CCComp extends GameComponent implements ecs.IComp { /** 从父节点移除自己 */ remove(params?: UIRemove) { const cct = ECSModel.compCtors[this.tid]; - ModuleUtil.remove(this.ent, cct, params); + if (this.ent) { + ModuleUtil.remove(this.ent, cct, params); + } + else { + console.error(`组件 ${this.name} 移除失败,实体不存在`); + } } abstract reset(): void; diff --git a/assets/module/common/CCVMParentComp.ts b/assets/module/common/CCVMParentComp.ts index d87514e..e9e6be4 100644 --- a/assets/module/common/CCVMParentComp.ts +++ b/assets/module/common/CCVMParentComp.ts @@ -57,7 +57,12 @@ export abstract class CCVMParentComp extends VMParent implements ecs.IComp { /** 从父节点移除自己 */ remove(params?: UIRemove) { const cct = ECSModel.compCtors[this.tid]; - ModuleUtil.remove(this.ent, cct, params); + if (this.ent) { + ModuleUtil.remove(this.ent, cct, params); + } + else { + console.error(`组件 ${this.name} 移除失败,实体不存在`); + } } abstract reset(): void; diff --git a/assets/module/common/ModuleUtil.ts b/assets/module/common/ModuleUtil.ts index 826fe05..239b7a5 100644 --- a/assets/module/common/ModuleUtil.ts +++ b/assets/module/common/ModuleUtil.ts @@ -1,6 +1,7 @@ import { Node, __private } from "cc"; import { oops } from "../../core/Oops"; import { resLoader } from "../../core/common/loader/ResLoader"; +import { gui } from "../../core/gui/Gui"; import { Uiid } from "../../core/gui/layer/LayerEnum"; import { LayerUIElement, UICallbacks, UIRemove } from "../../core/gui/layer/LayerUIElement"; import { ViewUtil } from "../../core/utils/ViewUtil"; @@ -33,7 +34,7 @@ export class ModuleUtil { }; //@ts-ignore - const key = ctor.oopsGuiKey; + const key = ctor[gui.internal.GUI_KEY]; if (key) { oops.gui.open(key, uiArgs, uic); } @@ -59,7 +60,7 @@ export class ModuleUtil { } //@ts-ignore - const key = ctor.oopsGuiKey; + const key = ctor[gui.internal.GUI_KEY]; if (key) { const node = oops.gui.get(key); if (node == null) {