From e935566dd4b8ff3776c6a9c72f405c23f71d104d Mon Sep 17 00:00:00 2001 From: dgflash Date: Wed, 21 May 2025 14:20:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E8=B5=84=E6=BA=90=E5=86=85?= =?UTF-8?q?=E5=AD=98=E8=87=AA=E5=8A=A8=E5=8C=96=E7=AE=A1=E7=90=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/module/common/GameComponent.ts | 34 ++++++++++++++++++++------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/assets/module/common/GameComponent.ts b/assets/module/common/GameComponent.ts index 2bb99be..235a26e 100644 --- a/assets/module/common/GameComponent.ts +++ b/assets/module/common/GameComponent.ts @@ -26,10 +26,13 @@ interface ResRecord { bundle: string, /** 资源路径 */ path: string, + /** 引用计数 */ + refCount: number, /** 资源编号 */ resId?: number } + /** * 游戏显示对象组件模板 * 1、当前对象加载的资源,会在对象释放时,自动释放引用的资源 @@ -148,8 +151,12 @@ export class GameComponent extends Component { for (let index = 0; index < paths.length; index++) { let realPath = paths[index]; let key = this.getResKey(realBundle, realPath, resId); - if (!rps.has(key)) { - rps.set(key, { path: realPath, bundle: realBundle, resId: resId }); + let rp = rps.get(key); + if (rp) { + rp.refCount++; + } + else { + rps.set(key, { path: realPath, bundle: realBundle, refCount: 1, resId: resId }); } } } @@ -157,16 +164,24 @@ export class GameComponent extends Component { let realBundle = bundleName; let realPath = paths; let key = this.getResKey(realBundle, realPath, resId); - if (!rps.has(key)) { - rps.set(key, { path: realPath, bundle: realBundle, resId: resId }); + let rp = rps.get(key); + if (rp) { + rp.refCount++; + } + else { + rps.set(key, { path: realPath, bundle: realBundle, refCount: 1, resId: resId }); } } else { let realBundle = oops.res.defaultBundleName; let realPath = bundleName; let key = this.getResKey(realBundle, realPath, resId); - if (!rps.has(key)) { - rps.set(key, { path: realPath, bundle: realBundle, resId: resId }); + let rp = rps.get(key); + if (rp) { + rp.refCount++; + } + else { + rps.set(key, { path: realPath, bundle: realBundle, refCount: 1, resId: resId }); } } } @@ -265,7 +280,9 @@ export class GameComponent extends Component { const rps = this.resPaths.get(ResType.Load); if (rps) { rps.forEach((value: ResRecord) => { - oops.res.release(value.path, value.bundle); + for (let i = 0; i < value.refCount; i++) { + oops.res.release(value.path, value.bundle); + } }); rps.clear(); } @@ -313,6 +330,7 @@ export class GameComponent extends Component { } return; } + spriteFrame.addRef(); target.spriteFrame = spriteFrame; } //#endregion @@ -348,7 +366,7 @@ export class GameComponent extends Component { if (bundleName == null) bundleName = oops.res.defaultBundleName; await oops.audio.playEffect(url, bundleName, () => { if (!this.isValid) return; - + const rps = this.resPaths.get(ResType.Audio); if (rps) { const key = this.getResKey(bundleName, url);