From dfc0690dd2a559c38f6dc1979436c8b9a41f4dbe Mon Sep 17 00:00:00 2001 From: dgflash Date: Tue, 14 Oct 2025 20:25:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/core/game/GameManager.ts | 7 +++---- assets/core/gui/layer/LayerGame.ts | 2 +- assets/module/common/CCEntity.ts | 26 ++++++++++++++++++++------ assets/module/common/GameComponent.ts | 10 +++++++--- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/assets/core/game/GameManager.ts b/assets/core/game/GameManager.ts index c491367..9ad8917 100644 --- a/assets/core/game/GameManager.ts +++ b/assets/core/game/GameManager.ts @@ -5,9 +5,9 @@ * @LastEditTime: 2022-09-02 12:09:55 */ import { Node, director } from 'cc'; -import { ViewUtil } from '../utils/ViewUtil'; -import { resLoader } from '../common/loader/ResLoader'; import { GameComponent } from '../../module/common/GameComponent'; +import { resLoader } from '../common/loader/ResLoader'; +import { ViewUtil } from '../utils/ViewUtil'; /** 游戏元素打开参数 */ export interface ElementParams { @@ -45,8 +45,7 @@ export class GameManager { let node: Node = null!; // 自动内存管理 if (parent instanceof GameComponent) { - await parent.load(bundleName, prefabPath); - node = parent.createPrefabNode(prefabPath, bundleName); + node = await parent.createPrefabNode(prefabPath, bundleName); node.parent = parent.node; } // 手动内存管理 diff --git a/assets/core/gui/layer/LayerGame.ts b/assets/core/gui/layer/LayerGame.ts index 319d552..3b4425c 100644 --- a/assets/core/gui/layer/LayerGame.ts +++ b/assets/core/gui/layer/LayerGame.ts @@ -99,7 +99,7 @@ export class LayerGame extends Node { resLoader.release(lge.params.config.prefab!, lge.params.config.bundle); } } - node.removeFromParent(); + node.destroy(); } } else { diff --git a/assets/module/common/CCEntity.ts b/assets/module/common/CCEntity.ts index 9849688..79135f2 100644 --- a/assets/module/common/CCEntity.ts +++ b/assets/module/common/CCEntity.ts @@ -10,6 +10,7 @@ import { CompType } from "../../libs/ecs/ECSModel"; import { CCBusiness } from "./CCBusiness"; import { CCView } from "./CCView"; import { CCViewVM } from "./CCViewVM"; +import { GameComponent } from "./GameComponent"; export type ECSCtor = __private.__types_globals__Constructor | __private.__types_globals__AbstractedConstructor; export type ECSView = CCViewVM | CCView; @@ -74,12 +75,25 @@ export abstract class CCEntity extends ecs.Entity { * @param path 显示资源地址 * @param bundleName 资源包名称 */ - addPrefab(ctor: ECSCtor, parent: Node, path: string, bundleName: string = resLoader.defaultBundleName): Node { - const node = ViewUtil.createPrefabNode(path, bundleName); - const comp = node.getComponent(ctor)!; - this.add(comp); - node.parent = parent; - return node; + addPrefab(ctor: ECSCtor, parent: Node | GameComponent, path: string, bundleName: string = resLoader.defaultBundleName): Promise { + return new Promise(async (resolve, reject) => { + let node: Node = null!; + // 跟随父节点施放自动释放当前资源 + if (parent instanceof GameComponent) { + node = await parent.createPrefabNode(path, bundleName); + const comp = node.getComponent(ctor)!; + this.add(comp); + node.parent = parent.node; + } + // 手动内存管理 + else { + node = await ViewUtil.createPrefabNodeAsync(path, bundleName); + const comp = node.getComponent(ctor)!; + this.add(comp); + node.parent = parent; + } + resolve(node); + }); } /** diff --git a/assets/module/common/GameComponent.ts b/assets/module/common/GameComponent.ts index aefafc9..c9ad13c 100644 --- a/assets/module/common/GameComponent.ts +++ b/assets/module/common/GameComponent.ts @@ -4,7 +4,7 @@ * @LastEditors: dgflash * @LastEditTime: 2022-12-13 11:36:00 */ -import { Asset, Button, Component, EventHandler, EventKeyboard, EventTouch, Input, Node, Sprite, SpriteFrame, __private, _decorator, input, isValid } from "cc"; +import { Asset, Button, Component, EventHandler, EventKeyboard, EventTouch, Input, Node, Prefab, Sprite, SpriteFrame, __private, _decorator, input, instantiate, isValid } from "cc"; import { oops } from "../../core/Oops"; import { AudioEffect } from "../../core/common/audio/AudioEffect"; import { IAudioParams } from "../../core/common/audio/IAudio"; @@ -99,8 +99,12 @@ export class GameComponent extends Component { * 从资源缓存中找到预制资源名并创建一个显示对象 * @param path 资源路径 */ - createPrefabNode(path: string, bundleName: string = oops.res.defaultBundleName): Node { - return ViewUtil.createPrefabNode(path, bundleName); + createPrefabNode(path: string, bundleName: string = oops.res.defaultBundleName): Promise { + return new Promise(async (resolve, reject) => { + const prefab = await this.load(bundleName, path, Prefab); + const node = instantiate(prefab); + resolve(node); + }); } //#endregion