mirror of
https://github.com/Gongxh0901/kunpolibrary
synced 2025-07-03 06:44:21 +00:00
添加按加载批次释放资源;发布1.0.31
This commit is contained in:
parent
43dbd5b803
commit
0f912897f4
@ -10,3 +10,5 @@
|
|||||||
## 1.0.30
|
## 1.0.30
|
||||||
- 修复已知bug
|
- 修复已知bug
|
||||||
- 调整微信和抖音小游戏广告实现
|
- 调整微信和抖音小游戏广告实现
|
||||||
|
## 1.0.31
|
||||||
|
- 修改资源加载器,自定义加载批次,支持按加载批次释放资源,详情见 [资源管理](./docs/Asset.md)
|
||||||
|
@ -3,8 +3,15 @@
|
|||||||
|
|
||||||
### 特点
|
### 特点
|
||||||
* 可通过路径或者uuid获取资源
|
* 可通过路径或者uuid获取资源
|
||||||
|
|
||||||
* 只适合手动管理资源,单无论加载多少次,卸载一次后删除
|
* 只适合手动管理资源,单无论加载多少次,卸载一次后删除
|
||||||
|
|
||||||
|
* 可根据 `new kunpo.AssetLoader("batchName")` 传入的 `batchName`批量卸载资源
|
||||||
|
|
||||||
|
> 比如进入战斗时,创建了多个new kunpo.AssetLoader("batchName") 来加载资源,传入的batchName相同
|
||||||
|
>
|
||||||
|
> 等退出战斗后,可以通过 AssetPool.releaseBatchAssets("batchName") 一键释放所有等于batchName的资源
|
||||||
|
|
||||||
### 使用
|
### 使用
|
||||||
```typescript
|
```typescript
|
||||||
let paths: kunpo.IAssetConfig[] = [
|
let paths: kunpo.IAssetConfig[] = [
|
||||||
@ -14,7 +21,7 @@
|
|||||||
{ path: "texture/6101/spriteFrame", type: cc.SpriteFrame, isFile: true },
|
{ path: "texture/6101/spriteFrame", type: cc.SpriteFrame, isFile: true },
|
||||||
{ path: "pet", type: cc.SpriteFrame, bundle: "bundle_res" },
|
{ path: "pet", type: cc.SpriteFrame, bundle: "bundle_res" },
|
||||||
];
|
];
|
||||||
let loader = new kunpo.AssetLoader("load");
|
let loader = new kunpo.AssetLoader("batchName");
|
||||||
loader.start({
|
loader.start({
|
||||||
configs: paths,
|
configs: paths,
|
||||||
complete: () => {
|
complete: () => {
|
||||||
@ -85,5 +92,11 @@ public static releaseUUID(uuid: string): void
|
|||||||
|
|
||||||
/** 释放所有加载的资源 */
|
/** 释放所有加载的资源 */
|
||||||
public static releaseAll(): void
|
public static releaseAll(): void
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按资源加载批次释放资源
|
||||||
|
* @param batchName 资源加载批次名 对应 AssetLoader 实例化时传入的 name
|
||||||
|
*/
|
||||||
|
public static releaseBatchAssets(batchName: string): void;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "kunpocc",
|
"name": "kunpocc",
|
||||||
"version": "1.0.30",
|
"version": "1.0.31",
|
||||||
"description": "基于creator3.0+的kunpocc库",
|
"description": "基于creator3.0+的kunpocc库",
|
||||||
"main": "./dist/kunpocc.cjs",
|
"main": "./dist/kunpocc.cjs",
|
||||||
"module": "./dist/kunpocc.mjs",
|
"module": "./dist/kunpocc.mjs",
|
||||||
|
@ -104,8 +104,8 @@ export class AssetLoader {
|
|||||||
*/
|
*/
|
||||||
private _completeCounts: Map<string, number> = new Map();
|
private _completeCounts: Map<string, number> = new Map();
|
||||||
|
|
||||||
constructor(name?: string) {
|
constructor(batchName?: string) {
|
||||||
this._name = name || "AssetLoader";
|
this._name = batchName || "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -293,7 +293,7 @@ export class AssetLoader {
|
|||||||
} else {
|
} else {
|
||||||
item.status = StateType.Finish;
|
item.status = StateType.Finish;
|
||||||
this._completeCounts.set(`${item.bundle}:${item.path}`, assets.length);
|
this._completeCounts.set(`${item.bundle}:${item.path}`, assets.length);
|
||||||
AssetPool.add(assets, bundle);
|
AssetPool.add(assets, bundle, this._name);
|
||||||
}
|
}
|
||||||
this._progress && this.updateProgress();
|
this._progress && this.updateProgress();
|
||||||
this.loadNext();
|
this.loadNext();
|
||||||
@ -314,7 +314,7 @@ export class AssetLoader {
|
|||||||
} else {
|
} else {
|
||||||
item.status = StateType.Finish;
|
item.status = StateType.Finish;
|
||||||
this._completeCounts.set(`${item.bundle}:${item.path}`, 1);
|
this._completeCounts.set(`${item.bundle}:${item.path}`, 1);
|
||||||
AssetPool.add(asset, bundle);
|
AssetPool.add(asset, bundle, this._name);
|
||||||
}
|
}
|
||||||
|
|
||||||
this._progress && this.updateProgress();
|
this._progress && this.updateProgress();
|
||||||
|
@ -9,16 +9,27 @@ import { log } from "../tool/log";
|
|||||||
import { AssetUtils } from "./AssetUtils";
|
import { AssetUtils } from "./AssetUtils";
|
||||||
|
|
||||||
export class AssetPool {
|
export class AssetPool {
|
||||||
/** @internal */
|
/**
|
||||||
|
* 资源名对应的资源
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
private static _assets: { [path: string]: Asset } = {};
|
private static _assets: { [path: string]: Asset } = {};
|
||||||
/** @internal */
|
/**
|
||||||
|
* uuid 对应的资源名
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
private static _uuidToName: Map<string, string> = new Map();
|
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)) {
|
if (Array.isArray(asset)) {
|
||||||
for (const item of asset) {
|
for (const item of asset) {
|
||||||
this.add(item, bundle);
|
this.add(item, bundle, batchName);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let uuid = asset.uuid;
|
let uuid = asset.uuid;
|
||||||
@ -32,6 +43,12 @@ export class AssetPool {
|
|||||||
// log(`>>>uuid:${uuid}, path:${info.path}`);
|
// log(`>>>uuid:${uuid}, path:${info.path}`);
|
||||||
this._uuidToName.set(uuid, key);
|
this._uuidToName.set(uuid, key);
|
||||||
this._assets[key] = asset;
|
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;
|
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 {
|
public static releasePath(path: string, bundlename: string = "resources"): void {
|
||||||
let key = this.getKey(path, bundlename);
|
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 {
|
public static releaseAll(): void {
|
||||||
for (const key in this._assets) {
|
for (const key in this._assets) {
|
||||||
@ -121,6 +144,20 @@ export class AssetPool {
|
|||||||
}
|
}
|
||||||
this._assets = {};
|
this._assets = {};
|
||||||
this._uuidToName.clear();
|
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];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user