添加按加载批次释放资源;发布1.0.31

This commit is contained in:
gongxh 2025-05-06 16:35:31 +08:00
parent 43dbd5b803
commit 0f912897f4
5 changed files with 72 additions and 20 deletions

View File

@ -9,4 +9,6 @@
- 添加原生平台热更新支持
## 1.0.30
- 修复已知bug
- 调整微信和抖音小游戏广告实现
- 调整微信和抖音小游戏广告实现
## 1.0.31
- 修改资源加载器,自定义加载批次,支持按加载批次释放资源,详情见 [资源管理](./docs/Asset.md)

View File

@ -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;
```

View File

@ -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",

View File

@ -104,8 +104,8 @@ export class AssetLoader {
*/
private _completeCounts: Map<string, number> = 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();

View File

@ -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<string, string> = new Map();
/**
*
* @internal
*/
private static _batchAssetNames: Map<string, string[]> = 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];
}
}
/**