From 0f912897f41f464214f50254afda6e5c26727752 Mon Sep 17 00:00:00 2001 From: gongxh Date: Tue, 6 May 2025 16:35:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8C=89=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E6=89=B9=E6=AC=A1=E9=87=8A=E6=94=BE=E8=B5=84=E6=BA=90;?= =?UTF-8?q?=E5=8F=91=E5=B8=831.0.31?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 ++- docs/Asset.md | 15 +++++++++- package.json | 2 +- src/asset/AssetLoader.ts | 8 ++--- src/asset/AssetPool.ts | 63 +++++++++++++++++++++++++++++++--------- 5 files changed, 72 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b35b78..79e01ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,4 +9,6 @@ - 添加原生平台热更新支持 ## 1.0.30 - 修复已知bug -- 调整微信和抖音小游戏广告实现 \ No newline at end of file +- 调整微信和抖音小游戏广告实现 +## 1.0.31 +- 修改资源加载器,自定义加载批次,支持按加载批次释放资源,详情见 [资源管理](./docs/Asset.md) diff --git a/docs/Asset.md b/docs/Asset.md index 42c37da..85cc457 100644 --- a/docs/Asset.md +++ b/docs/Asset.md @@ -3,8 +3,15 @@ ### 特点 * 可通过路径或者uuid获取资源 + * 只适合手动管理资源,单无论加载多少次,卸载一次后删除 + * 可根据 `new kunpo.AssetLoader("batchName")` 传入的 `batchName`批量卸载资源 + + > 比如进入战斗时,创建了多个new kunpo.AssetLoader("batchName") 来加载资源,传入的batchName相同 + > + > 等退出战斗后,可以通过 AssetPool.releaseBatchAssets("batchName") 一键释放所有等于batchName的资源 + ### 使用 ```typescript let paths: kunpo.IAssetConfig[] = [ @@ -14,7 +21,7 @@ { path: "texture/6101/spriteFrame", type: cc.SpriteFrame, isFile: true }, { path: "pet", type: cc.SpriteFrame, bundle: "bundle_res" }, ]; - let loader = new kunpo.AssetLoader("load"); + let loader = new kunpo.AssetLoader("batchName"); loader.start({ configs: paths, complete: () => { @@ -85,5 +92,11 @@ public static releaseUUID(uuid: string): void /** 释放所有加载的资源 */ public static releaseAll(): void + +/** + * 按资源加载批次释放资源 + * @param batchName 资源加载批次名 对应 AssetLoader 实例化时传入的 name + */ +public static releaseBatchAssets(batchName: string): void; ``` diff --git a/package.json b/package.json index 78f5634..8952bdb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kunpocc", - "version": "1.0.30", + "version": "1.0.31", "description": "基于creator3.0+的kunpocc库", "main": "./dist/kunpocc.cjs", "module": "./dist/kunpocc.mjs", diff --git a/src/asset/AssetLoader.ts b/src/asset/AssetLoader.ts index 12d3f37..9035ea5 100644 --- a/src/asset/AssetLoader.ts +++ b/src/asset/AssetLoader.ts @@ -104,8 +104,8 @@ export class AssetLoader { */ private _completeCounts: Map = new Map(); - constructor(name?: string) { - this._name = name || "AssetLoader"; + constructor(batchName?: string) { + this._name = batchName || ""; } /** @@ -293,7 +293,7 @@ export class AssetLoader { } else { item.status = StateType.Finish; this._completeCounts.set(`${item.bundle}:${item.path}`, assets.length); - AssetPool.add(assets, bundle); + AssetPool.add(assets, bundle, this._name); } this._progress && this.updateProgress(); this.loadNext(); @@ -314,7 +314,7 @@ export class AssetLoader { } else { item.status = StateType.Finish; this._completeCounts.set(`${item.bundle}:${item.path}`, 1); - AssetPool.add(asset, bundle); + AssetPool.add(asset, bundle, this._name); } this._progress && this.updateProgress(); diff --git a/src/asset/AssetPool.ts b/src/asset/AssetPool.ts index f2ab82a..f4d97c0 100644 --- a/src/asset/AssetPool.ts +++ b/src/asset/AssetPool.ts @@ -9,16 +9,27 @@ import { log } from "../tool/log"; import { AssetUtils } from "./AssetUtils"; export class AssetPool { - /** @internal */ + /** + * 资源名对应的资源 + * @internal + */ private static _assets: { [path: string]: Asset } = {}; - /** @internal */ + /** + * uuid 对应的资源名 + * @internal + */ private static _uuidToName: Map = new Map(); + /** + * 资源加载批次对应的资源名 + * @internal + */ + private static _batchAssetNames: Map = new Map(); /** 批量添加资源 */ - public static add(asset: Asset[] | Asset, bundle: AssetManager.Bundle = resources): void { + public static add(asset: Asset[] | Asset, bundle: AssetManager.Bundle = resources, batchName: string = ""): void { if (Array.isArray(asset)) { for (const item of asset) { - this.add(item, bundle); + this.add(item, bundle, batchName); } } else { let uuid = asset.uuid; @@ -32,6 +43,12 @@ export class AssetPool { // log(`>>>uuid:${uuid}, path:${info.path}`); this._uuidToName.set(uuid, key); this._assets[key] = asset; + + if (batchName) { + let names = this._batchAssetNames.get(batchName) || []; + names.push(key); + this._batchAssetNames.set(batchName, names); + } } } @@ -68,6 +85,21 @@ export class AssetPool { return this._assets[key] as T; } + /** + * 按资源加载批次释放资源 + * @param batchName 资源加载批次名 对应 AssetLoader 实例化时传入的 name + */ + public static releaseBatchAssets(batchName: string): void { + if (!this._batchAssetNames.has(batchName)) { + return; + } + let names = this._batchAssetNames.get(batchName); + for (const name of names) { + this.release(name); + } + this._batchAssetNames.delete(batchName); + } + /** 按资源路径释放资源 */ public static releasePath(path: string, bundlename: string = "resources"): void { let key = this.getKey(path, bundlename); @@ -105,15 +137,6 @@ export class AssetPool { } } - private static release(key: string): void { - if (this._assets[key]) { - this._uuidToName.delete(this._assets[key].uuid); - - this._assets[key].decRef(); - delete this._assets[key]; - } - } - /** 释放所有加载的资源 */ public static releaseAll(): void { for (const key in this._assets) { @@ -121,6 +144,20 @@ export class AssetPool { } this._assets = {}; this._uuidToName.clear(); + this._batchAssetNames.clear(); + } + + /** + * 按key释放资源 + * @internal + */ + private static release(key: string): void { + if (this._assets[key]) { + this._uuidToName.delete(this._assets[key].uuid); + + this._assets[key].decRef(); + delete this._assets[key]; + } } /**