From fafe542e7038efb76f8c4291e14e20d60f47cc68 Mon Sep 17 00:00:00 2001 From: donggang <> Date: Wed, 26 Jun 2024 11:41:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96GameComponent=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=9C=A8=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=89=8D=E6=89=8D=E5=88=9B=E5=BB=BA=E5=AF=B9=E5=BA=94=E7=9A=84?= =?UTF-8?q?=E5=86=85=E5=AD=98=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/module/common/GameComponent.ts | 79 +++++++++++++++++---------- 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/assets/module/common/GameComponent.ts b/assets/module/common/GameComponent.ts index b877462..a2088dd 100644 --- a/assets/module/common/GameComponent.ts +++ b/assets/module/common/GameComponent.ts @@ -59,18 +59,22 @@ export class GameComponent extends Component { //#region 预制节点管理 - // /** 摊平的节点集合(不能重名) */ - // private nodes: Map = new Map(); + /** 摊平的节点集合(所有节点不能重名) */ + private nodes: Map = null!; - // /** 通过节点名获取预制上的节点,整个预制不能有重名节点 */ - // getNode(name: string): Node | undefined { - // return this.nodes.get(name); - // } + /** 通过节点名获取预制上的节点,整个预制不能有重名节点 */ + getNode(name: string): Node | undefined { + if (this.nodes) { + return this.nodes.get(name); + } + return undefined; + } - // /** 平摊所有节点存到Map中通过get(name: string)方法获取 */ - // nodeTreeInfoLite() { - // ViewUtil.nodeTreeInfoLite(this.node, this.nodes); - // } + /** 平摊所有节点存到Map中通过get(name: string)方法获取 */ + nodeTreeInfoLite() { + this.nodes = new Map(); + ViewUtil.nodeTreeInfoLite(this.node, this.nodes); + } /** * 从资源缓存中找到预制资源名并创建一个显示对象 @@ -104,9 +108,9 @@ export class GameComponent extends Component { //#region 资源加载管理 /** 资源路径 */ - private resPaths: Map = new Map(); // 游戏资源 - private resPathsDir: Map = new Map(); // 游戏资源文件夹 - private resPathsAudioEffect: Map = new Map(); // 音效类资源 + private resPaths: Map = null!; // 游戏资源 + private resPathsDir: Map = null!; // 游戏资源文件夹 + private resPathsAudioEffect: Map = null!; // 音效类资源 /** * 获取资源 @@ -132,6 +136,8 @@ export class GameComponent extends Component { paths?: string | string[] | AssetType | ProgressCallback | CompleteCallback | null, type?: AssetType | ProgressCallback | CompleteCallback | null, ) { + if (this.resPaths == null) this.resPaths = new Map(); + if (paths instanceof Array) { paths.forEach(path => { this.resPaths.set(path, oops.res.defaultBundleName); @@ -159,6 +165,8 @@ export class GameComponent extends Component { onProgress?: ProgressCallback | CompleteCallback | null, onComplete?: CompleteCallback | null, ) { + if (this.resPaths == null) this.resPaths = new Map(); + if (paths instanceof Array) { paths.forEach(path => { this.resPaths.set(path, oops.res.defaultBundleName); @@ -186,6 +194,8 @@ export class GameComponent extends Component { onProgress?: ProgressCallback | CompleteCallback | null, onComplete?: CompleteCallback | null, ) { + if (this.resPathsDir == null) this.resPathsDir = new Map(); + if (typeof dir === "string") { this.resPathsDir.set(dir, oops.res.defaultBundleName); } @@ -197,29 +207,35 @@ export class GameComponent extends Component { /** 释放一个资源 */ release() { - this.resPaths.forEach((value: string, key: string) => { - oops.res.release(key, value); - }); - this.resPaths.clear(); - this.resPaths = null!; + if (this.resPaths) { + this.resPaths.forEach((value: string, key: string) => { + oops.res.release(key, value); + }); + this.resPaths.clear(); + this.resPaths = null!; + } } /** 释放一个文件夹的资源 */ releaseDir() { - this.resPathsDir.forEach((value: string, key: string) => { - oops.res.releaseDir(key, value); - }); - this.resPathsDir.clear(); - this.resPathsDir = null!; + if (this.resPathsDir) { + this.resPathsDir.forEach((value: string, key: string) => { + oops.res.releaseDir(key, value); + }); + this.resPathsDir.clear(); + this.resPathsDir = null!; + } } /** 释放音效资源 */ releaseAudioEffect() { - this.resPathsAudioEffect.forEach((value: string, key: string) => { - oops.audio.effect.release(key); - }); - this.resPathsAudioEffect.clear(); - this.resPathsAudioEffect = null!; + if (this.resPathsAudioEffect) { + this.resPathsAudioEffect.forEach((value: string, key: string) => { + oops.audio.effect.release(key); + }); + this.resPathsAudioEffect.clear(); + this.resPathsAudioEffect = null!; + } } //#endregion @@ -246,6 +262,8 @@ export class GameComponent extends Component { * @param url 资源地址 */ playEffect(url: string) { + if (this.resPathsAudioEffect == null) this.resPathsAudioEffect = new Map(); + this.resPathsAudioEffect.set(url, oops.res.defaultBundleName); oops.audio.playEffect(url); } @@ -388,7 +406,10 @@ export class GameComponent extends Component { } // 节点引用数据清除 - // this.nodes.clear(); + if (this.nodes) { + this.nodes.clear(); + this.nodes = null!; + } // 自动释放资源 this.releaseAudioEffect();