From 17d07a40b0b9131c9f578ce589e4cceedc9b28d4 Mon Sep 17 00:00:00 2001 From: "CHINAMI-57BHDMI\\Administrator" Date: Mon, 1 Jul 2024 11:48:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9GameComponent=E4=B8=ADpath?= =?UTF-8?q?=E7=9A=84=E5=8F=82=E6=95=B0=E9=94=99=E8=AF=AF=EF=BC=8C=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E5=9B=A0=E4=B8=BA=E9=94=99=E8=AF=AF=E7=9A=84=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=AF=BC=E8=87=B4=E6=97=A0=E6=B3=95=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=B4=A2=E5=BC=95=E4=BB=8E=E8=80=8C=E4=BD=BF?= =?UTF-8?q?=E5=BE=97=E5=9C=A8=E6=B8=85=E6=A5=9A=E8=B5=84=E6=BA=90=E6=97=B6?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E6=AD=A3=E7=A1=AE=E7=B4=A2=E5=BC=95=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20=E6=94=AF=E6=8C=81=E4=BC=A0=E5=85=A5?= =?UTF-8?q?=E4=B8=8D=E5=90=8Cbundle=E7=9A=84=E5=90=8C=E5=90=8D=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=90=8D=E6=88=96=E8=80=85=E8=B7=AF=E5=BE=84=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/module/common/GameComponent.ts | 101 ++++++++++++++++---------- 1 file changed, 63 insertions(+), 38 deletions(-) diff --git a/assets/module/common/GameComponent.ts b/assets/module/common/GameComponent.ts index cae12d0..c809f57 100644 --- a/assets/module/common/GameComponent.ts +++ b/assets/module/common/GameComponent.ts @@ -108,8 +108,8 @@ export class GameComponent extends Component { //#region 资源加载管理 /** 资源路径 */ - private resPaths: Map = null!; // 游戏资源 - private resPathsDir: Map = null!; // 游戏资源文件夹 + private resPathArray: Array<{bundle: string, path: string}> = null; // 游戏资源 + private resPathDirArray: Array<{bundle: string, path: string}> = null; private resPathsAudioEffect: Map = null!; // 音效类资源 /** @@ -122,6 +122,41 @@ export class GameComponent extends Component { return oops.res.get(path, type, bundleName); } + private fetchPath(arr: any[], realBundle: string, realDir: string){ + for (let index = 0; index < this.resPathDirArray.length; index++){ + let data = this.resPathDirArray[index]; + if (data.bundle == realBundle && data.path == realDir){ + return true; + } + } + return false; + } + + private addPathToRecord(bundleName: string, paths?: string | string[] | AssetType | ProgressCallback | CompleteCallback | null){ + if (this.resPathArray == null) this.resPathArray = new Array(); + if (paths instanceof Array) { + let realBundle = bundleName; + for (let index = 0; index < paths.length; index++){ + let realPath = paths[index]; + if (!this.fetchPath(this.resPathArray, realBundle, realPath)){ + this.resPathArray.push({path: realPath, bundle: realBundle}); + } + } + }else if (typeof paths === "string"){ + let realBundle = bundleName; + let realPath = paths; + if (!this.fetchPath(this.resPathArray, realBundle, realPath)){ + this.resPathArray.push({path: realPath, bundle: realBundle}); + } + }else { + let realBundle = oops.res.defaultBundleName; + let realPath = bundleName; + if (!this.fetchPath(this.resPathArray, realBundle, realPath)){ + this.resPathArray.push({path: realPath, bundle: realBundle}); + } + } + } + /** 异步加载资源 */ loadAsync(bundleName: string, paths: string | string[], type: AssetType | null): Promise; loadAsync(bundleName: string, paths: string | string[]): Promise; @@ -132,16 +167,7 @@ export class GameComponent extends Component { loadAsync(paths: string | string[]): Promise; loadAsync(paths: string | string[], type: AssetType | null): Promise; loadAsync(bundleName: string, paths?: string | string[] | AssetType | ProgressCallback | CompleteCallback | null, type?: AssetType | ProgressCallback | CompleteCallback | null): Promise { - if (this.resPaths == null) this.resPaths = new Map(); - - if (paths instanceof Array) { - paths.forEach(path => { - this.resPaths.set(path, bundleName); - }); - } - else { - this.resPaths.set(bundleName, oops.res.defaultBundleName); - } + this.addPathToRecord(bundleName, paths); return oops.res.loadAsync(bundleName, paths, type); } @@ -161,16 +187,7 @@ 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, bundleName); - }); - } - else { - this.resPaths.set(bundleName, oops.res.defaultBundleName); - } + this.addPathToRecord(bundleName, paths); oops.res.load(bundleName, paths, type, onProgress, onComplete); } @@ -190,36 +207,44 @@ export class GameComponent extends Component { onProgress?: ProgressCallback | CompleteCallback | null, onComplete?: CompleteCallback | null, ) { - if (this.resPathsDir == null) this.resPathsDir = new Map(); + if (this.resPathDirArray == null) this.resPathDirArray = new Array(); + let realDir: string; + let realBundle: string; if (typeof dir === "string") { - this.resPathsDir.set(dir, bundleName); + realDir = dir; + realBundle = bundleName; + } else { + realDir = bundleName; + realBundle = oops.res.defaultBundleName; } - else { - this.resPathsDir.set(bundleName, oops.res.defaultBundleName); + if (!this.fetchPath(this.resPathDirArray, realBundle, realDir)){ + this.resPathDirArray.push({bundle: realBundle, path: realDir}); } oops.res.loadDir(bundleName, dir, type, onProgress, onComplete); } /** 释放一个资源 */ release() { - if (this.resPaths) { - this.resPaths.forEach((value: string, key: string) => { - oops.res.release(key, value); - }); - this.resPaths.clear(); - this.resPaths = null!; + if (this.resPathArray) { + for (let index = 0; index < this.resPathArray.length; index++){ + let data = this.resPathArray[index]; + oops.res.release(data.path, data.bundle); + } + this.resPathArray.splice(0); + this.resPathArray = null!; } } /** 释放一个文件夹的资源 */ releaseDir() { - if (this.resPathsDir) { - this.resPathsDir.forEach((value: string, key: string) => { - oops.res.releaseDir(key, value); - }); - this.resPathsDir.clear(); - this.resPathsDir = null!; + if (this.resPathDirArray) { + for (let index = 0; index < this.resPathDirArray.length; index++){ + let data = this.resPathDirArray[index]; + oops.res.releaseDir(data.path, data.bundle); + } + this.resPathDirArray.splice(0); + this.resPathDirArray = null!; } }