mirror of
https://github.com/Gongxh0901/kunpolibrary
synced 2025-10-09 16:45:45 +00:00
添加按加载批次释放资源;发布1.0.31
This commit is contained in:
@@ -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();
|
||||
|
@@ -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];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user