diff --git a/assets/core/utils/ViewUtil.ts b/assets/core/utils/ViewUtil.ts index a051130..f0afa15 100644 --- a/assets/core/utils/ViewUtil.ts +++ b/assets/core/utils/ViewUtil.ts @@ -88,7 +88,7 @@ export class ViewUtil { } /** - * 从资源缓存中找到预制资源名并创建一个显示对象 + * 从资源缓存中找到预制资源名并创建一个显示对象(建议使用GameComponent里的同名方法,能自动管理内存施放) * @param path 资源路径 */ static createPrefabNode(path: string): Node { @@ -98,7 +98,7 @@ export class ViewUtil { } /** - * 加载预制并创建预制节点 + * 加载预制并创建预制节点(建议使用GameComponent里的同名方法,能自动管理内存施放) * @param path 资源路径 */ static createPrefabNodeAsync(path: string): Promise { @@ -115,23 +115,6 @@ export class ViewUtil { }); } - /** - * 加载预制节点 - * @param path 资源路径 - * @param callback 资源加载完成回调 - */ - static loadPrefabNode(path: string, callback: Function) { - oops.res.load(path, Prefab, (err: Error | null, content: Prefab) => { - if (err) { - console.error(`名为【${path}】的资源加载失败`); - return; - } - - var node = this.createPrefabNode(path); - callback(node); - }); - } - /** * 添加节点动画 * @param path 资源路径 diff --git a/assets/module/common/GameComponent.ts b/assets/module/common/GameComponent.ts index 43a9c71..b877462 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, __private, _decorator, input } from "cc"; +import { Asset, Button, Component, EventHandler, EventKeyboard, EventTouch, Input, Node, Prefab, __private, _decorator, input, instantiate } from "cc"; import { oops } from "../../core/Oops"; import { EventDispatcher } from "../../core/common/event/EventDispatcher"; import { EventMessage, ListenerFunc } from "../../core/common/event/EventMessage"; @@ -58,17 +58,47 @@ export class GameComponent extends Component { //#endregion //#region 预制节点管理 - /** 摊平的节点集合(不能重名) */ - private nodes: Map = new Map(); - /** 通过节点名获取预制上的节点,整个预制不能有重名节点 */ - getNode(name: string): Node | undefined { - return this.nodes.get(name); + // /** 摊平的节点集合(不能重名) */ + // private nodes: Map = new Map(); + + // /** 通过节点名获取预制上的节点,整个预制不能有重名节点 */ + // getNode(name: string): Node | undefined { + // return this.nodes.get(name); + // } + + // /** 平摊所有节点存到Map中通过get(name: string)方法获取 */ + // nodeTreeInfoLite() { + // ViewUtil.nodeTreeInfoLite(this.node, this.nodes); + // } + + /** + * 从资源缓存中找到预制资源名并创建一个显示对象 + * @param path 资源路径 + */ + createPrefabNode(path: string): Node { + var p: Prefab = oops.res.get(path, Prefab)!; + var n = instantiate(p); + return n; } - /** 平摊所有节点存到Map中通过get(name: string)方法获取 */ - nodeTreeInfoLite() { - ViewUtil.nodeTreeInfoLite(this.node, this.nodes); + /** + * 加载预制并创建预制节点 + * @param path 资源路径 + */ + createPrefabNodeAsync(path: string): Promise { + return new Promise((resolve, reject) => { + this.load(path, Prefab, (err: Error | null, content: Prefab) => { + if (err) { + console.error(`名为【${path}】的资源加载失败`); + resolve(null!); + } + else { + var node = instantiate(content); + resolve(node); + } + }); + }); } //#endregion @@ -358,7 +388,7 @@ export class GameComponent extends Component { } // 节点引用数据清除 - this.nodes.clear(); + // this.nodes.clear(); // 自动释放资源 this.releaseAudioEffect();