From 6936e09f89ba858e8375553186daeca226e1edf5 Mon Sep 17 00:00:00 2001 From: dgflash Date: Wed, 20 Aug 2025 09:26:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96GUI=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/core/gui/layer/LayerGame.ts | 12 ++----- assets/core/gui/layer/LayerHelper.ts | 20 +++++++++++ assets/core/gui/layer/LayerHelper.ts.meta | 9 +++++ assets/core/gui/layer/LayerManager.ts | 6 ++-- assets/core/gui/layer/LayerNotify.ts | 15 +++----- assets/core/gui/layer/LayerPopup.ts | 6 ++-- assets/core/gui/layer/LayerUI.ts | 44 ++++++++++------------- assets/core/gui/layer/LayerUIElement.ts | 4 +-- 8 files changed, 62 insertions(+), 54 deletions(-) create mode 100644 assets/core/gui/layer/LayerHelper.ts create mode 100644 assets/core/gui/layer/LayerHelper.ts.meta diff --git a/assets/core/gui/layer/LayerGame.ts b/assets/core/gui/layer/LayerGame.ts index c16ef28..0299ba4 100644 --- a/assets/core/gui/layer/LayerGame.ts +++ b/assets/core/gui/layer/LayerGame.ts @@ -4,11 +4,12 @@ * @LastEditors: dgflash * @LastEditTime: 2025-08-15 10:06:47 */ -import { Layers, Node, NodePool, Prefab, Vec3, warn, Widget } from "cc"; +import { Node, NodePool, Prefab, Vec3, warn } from "cc"; import { resLoader } from "../../common/loader/ResLoader"; import { ViewUtil } from "../../utils/ViewUtil"; import { LayerCustomType } from "./LayerEnum"; import { GameElementParams, LayerGameElement } from "./LayerGameElement"; +import { LayerHelper } from "./LayerHelper"; import { GameElementConfig } from "./UIConfig"; /* 二维游戏层 */ @@ -18,14 +19,7 @@ export class LayerGame extends Node { constructor() { super(LayerCustomType.Game); - - const widget: Widget = this.addComponent(Widget); - widget.isAlignLeft = widget.isAlignRight = widget.isAlignTop = widget.isAlignBottom = true; - widget.left = widget.right = widget.top = widget.bottom = 0; - widget.alignMode = 2; - widget.enabled = true; - - this.layer = Layers.Enum.UI_2D; + LayerHelper.setFullScreen(this); } /** diff --git a/assets/core/gui/layer/LayerHelper.ts b/assets/core/gui/layer/LayerHelper.ts new file mode 100644 index 0000000..05c71b5 --- /dev/null +++ b/assets/core/gui/layer/LayerHelper.ts @@ -0,0 +1,20 @@ +import { Layers } from "cc"; +import { Node, Widget } from "cc"; + +/** 界面层辅助工具 */ +export class LayerHelper { + /** + * 界面层全屏布局 + * @param node 全屏布局的节点 + */ + static setFullScreen(node: Node) { + const widget: Widget = node.addComponent(Widget); + widget.isAlignLeft = widget.isAlignRight = widget.isAlignTop = widget.isAlignBottom = true; + widget.left = widget.right = widget.top = widget.bottom = 0; + widget.alignMode = 2; + widget.enabled = true; + + node.layer = Layers.Enum.UI_2D; + return widget; + } +} \ No newline at end of file diff --git a/assets/core/gui/layer/LayerHelper.ts.meta b/assets/core/gui/layer/LayerHelper.ts.meta new file mode 100644 index 0000000..3f8102d --- /dev/null +++ b/assets/core/gui/layer/LayerHelper.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "2e65136e-fc22-466b-a076-16dfbb12b505", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/core/gui/layer/LayerManager.ts b/assets/core/gui/layer/LayerManager.ts index c3666ab..3d6e7a9 100644 --- a/assets/core/gui/layer/LayerManager.ts +++ b/assets/core/gui/layer/LayerManager.ts @@ -1,14 +1,14 @@ import { Camera, Layers, Node, ResolutionPolicy, SafeArea, Widget, screen, view, warn } from "cc"; import { resLoader } from "../../common/loader/ResLoader"; import { oops } from "../../Oops"; -import { LayerUIElement, UICallbacks } from "./LayerUIElement"; import { LayerDialog } from "./LayerDialog"; import { LayerCustomType, LayerTypeCls, UIConfigMap, Uiid } from "./LayerEnum"; -import { LayerGame } from "./LayerGame"; import { LayerNotify } from "./LayerNotify"; import { LayerPopUp } from "./LayerPopup"; import { LayerUI } from "./LayerUI"; +import { LayerUIElement, UICallbacks } from "./LayerUIElement"; import { UIConfig } from "./UIConfig"; +import { LayerGame } from "./LayerGame"; /** 界面层级管理器 */ export class LayerManager { @@ -372,7 +372,7 @@ export class LayerManager { * @example * oops.gui.clear(); */ - clear(isDestroy: boolean = false) { + clear(isDestroy: boolean = true) { this.uiLayers.forEach((layer: LayerUI) => { layer.clear(isDestroy); }) diff --git a/assets/core/gui/layer/LayerNotify.ts b/assets/core/gui/layer/LayerNotify.ts index e57c7e2..3058597 100644 --- a/assets/core/gui/layer/LayerNotify.ts +++ b/assets/core/gui/layer/LayerNotify.ts @@ -4,15 +4,14 @@ * @LastEditors: dgflash * @LastEditTime: 2022-09-02 13:44:12 */ -import { BlockInputEvents, Layers, Node, Widget, instantiate } from "cc"; +import { BlockInputEvents, Node, instantiate } from "cc"; import { EDITOR } from "cc/env"; import { ViewUtil } from "../../utils/ViewUtil"; import { PromptResType } from "../GuiEnum"; import { Notify } from "../prompt/Notify"; +import { LayerHelper } from "./LayerHelper"; -/* - * 滚动消息提示层 - */ +/* 滚动消息提示层 */ export class LayerNotify extends Node { private black!: BlockInputEvents; /** 等待提示资源 */ @@ -24,14 +23,8 @@ export class LayerNotify extends Node { constructor(name: string) { super(name); + LayerHelper.setFullScreen(this); - const widget: Widget = this.addComponent(Widget); - widget.isAlignLeft = widget.isAlignRight = widget.isAlignTop = widget.isAlignBottom = true; - widget.left = widget.right = widget.top = widget.bottom = 0; - widget.alignMode = 2; - widget.enabled = true; - - this.layer = Layers.Enum.UI_2D; this.black = this.addComponent(BlockInputEvents); this.black.enabled = false; } diff --git a/assets/core/gui/layer/LayerPopup.ts b/assets/core/gui/layer/LayerPopup.ts index aaa7ab4..c950327 100644 --- a/assets/core/gui/layer/LayerPopup.ts +++ b/assets/core/gui/layer/LayerPopup.ts @@ -3,13 +3,12 @@ * @LastEditors: dgflash * @LastEditTime: 2022-09-02 13:44:28 */ - -import { BlockInputEvents, EventTouch, Layers, Node } from "cc"; +import { BlockInputEvents, EventTouch, Node } from "cc"; import { ViewUtil } from "../../utils/ViewUtil"; import { PromptResType } from "../GuiEnum"; import { LayerUI } from "./LayerUI"; -import { UIConfig } from "./UIConfig"; import { UIParams } from "./LayerUIElement"; +import { UIConfig } from "./UIConfig"; /* 弹窗层,允许同时弹出多个窗口 */ export class LayerPopUp extends LayerUI { @@ -21,7 +20,6 @@ export class LayerPopUp extends LayerUI { constructor(name: string) { super(name); - this.layer = Layers.Enum.UI_2D; this.on(Node.EventType.CHILD_ADDED, this.onChildAdded, this); this.on(Node.EventType.CHILD_REMOVED, this.onChildRemoved, this); } diff --git a/assets/core/gui/layer/LayerUI.ts b/assets/core/gui/layer/LayerUI.ts index fc5fb57..ebf9446 100644 --- a/assets/core/gui/layer/LayerUI.ts +++ b/assets/core/gui/layer/LayerUI.ts @@ -1,7 +1,8 @@ -import { instantiate, Node, Prefab, SafeArea, Widget } from "cc"; +import { instantiate, Node, Prefab, SafeArea } from "cc"; import { Collection } from "db://oops-framework/libs/collection/Collection"; import { oops } from "../../Oops"; import { Uiid } from "./LayerEnum"; +import { LayerHelper } from "./LayerHelper"; import { LayerUIElement, UICallbacks, UIParams } from "./LayerUIElement"; import { UIConfig } from "./UIConfig"; @@ -20,12 +21,7 @@ export class LayerUI extends Node { */ constructor(name: string) { super(name); - - const widget: Widget = this.addComponent(Widget); - widget.isAlignLeft = widget.isAlignRight = widget.isAlignTop = widget.isAlignBottom = true; - widget.left = widget.right = widget.top = widget.bottom = 0; - widget.alignMode = 2; - widget.enabled = true; + LayerHelper.setFullScreen(this); } /** @@ -147,20 +143,20 @@ export class LayerUI extends Node { if (isDestroy !== undefined) release = isDestroy; // 界面移出舞台 - const vp = this.ui_nodes.get(prefabPath); - if (vp) { + const uip = this.ui_nodes.get(prefabPath); + if (uip) { // 优先使用参数中控制的释放条件,如果未传递参数则用配置中的释放条件,默认不缓存关闭的界面 if (release === undefined) { - release = vp.config.destroy !== undefined ? vp.config.destroy : true; + release = uip.config.destroy !== undefined ? uip.config.destroy : true; } // 不释放界面,缓存起来待下次使用 if (release === false) { - this.ui_cache.set(vp.config.prefab, vp); + this.ui_cache.set(uip.config.prefab, uip); } - const childNode = vp.node; - const comp = childNode.getComponent(LayerUIElement)!; + const node = uip.node; + const comp = node.getComponent(LayerUIElement)!; comp.remove(release); } @@ -174,12 +170,10 @@ export class LayerUI extends Node { if (vp) { this.onCloseWindow(vp); this.ui_cache.delete(prefabPath); - const childNode = vp.node; - const comp = childNode.getComponent(LayerUIElement)!; - if (comp) { - comp.remove(true); - } - childNode.destroy(); + const node = vp.node; + const comp = node.getComponent(LayerUIElement)!; + comp.remove(true); + node.destroy(); } } @@ -189,8 +183,7 @@ export class LayerUI extends Node { */ get(prefabPath: string): Node { const vp = this.ui_nodes.get(prefabPath); - if (vp) - return vp.node; + if (vp) return vp.node; return null!; } @@ -208,10 +201,11 @@ export class LayerUI extends Node { */ clear(isDestroy: boolean): void { // 清除所有显示的界面 - this.ui_nodes.forEach((value: UIParams, key: string) => { - this.remove(value.config.prefab, isDestroy); - value.valid = false; - }); + const length = this.ui_nodes.array.length - 1; + for (let i = length; i >= 0; i--) { + const uip = this.ui_nodes.array[i]; + this.remove(uip.config.prefab, isDestroy); + } this.ui_nodes.clear(); // 清除缓存中的界面 diff --git a/assets/core/gui/layer/LayerUIElement.ts b/assets/core/gui/layer/LayerUIElement.ts index 8c8c6b5..6c9c8c9 100644 --- a/assets/core/gui/layer/LayerUIElement.ts +++ b/assets/core/gui/layer/LayerUIElement.ts @@ -8,12 +8,12 @@ import { Component, Node, _decorator } from "cc"; import { oops } from "../../Oops"; import { UIConfig } from "./UIConfig"; -const { ccclass } = _decorator; - const EventOnAdded: string = "onAdded"; const EventOnBeforeRemove: string = "onBeforeRemove"; const EventOnRemoved: string = "onRemoved"; +const { ccclass } = _decorator; + /** 窗口元素组件 */ @ccclass('LayerUIElement') export class LayerUIElement extends Component {