mirror of
https://github.com/wyb10a10/cocos_creator_framework.git
synced 2026-05-11 08:10:52 +08:00
144 lines
7.4 KiB
TypeScript
144 lines
7.4 KiB
TypeScript
/**
|
||
* ResLoader2,封装资源的加载和卸载接口,隐藏新老资源底层差异
|
||
* 1. 加载资源接口
|
||
* 2. 卸载资源接口
|
||
*
|
||
* 2021-1-24 by 宝爷
|
||
*/
|
||
|
||
import { Asset, js, error, Constructor, resources, __private, assetManager, AssetManager } from "cc";
|
||
export type ProgressCallback = __private.cocos_core_asset_manager_shared_ProgressCallback;
|
||
export type CompleteCallback<T = any> = __private.cocos_core_asset_manager_shared_CompleteCallbackWithData;
|
||
export type IRemoteOptions = __private.cocos_core_asset_manager_shared_IRemoteOptions;
|
||
export type AssetType<T = Asset> = Constructor<T>;
|
||
|
||
interface ILoadResArgs<T extends Asset> {
|
||
bundle?: string;
|
||
dir?: string;
|
||
paths: string | string[];
|
||
type: AssetType<T> | null;
|
||
onProgress: ProgressCallback | null;
|
||
onComplete: CompleteCallback<T> | null;
|
||
}
|
||
|
||
export default class ResLoader {
|
||
|
||
public parseLoadResArgs<T extends Asset>(
|
||
paths: string | string[],
|
||
type?: AssetType<T> | ProgressCallback | CompleteCallback | null,
|
||
onProgress?: AssetType<T> | ProgressCallback | CompleteCallback | null,
|
||
onComplete?: ProgressCallback | CompleteCallback | null
|
||
) {
|
||
let pathsOut: any = paths;
|
||
let typeOut: any = type;
|
||
let onProgressOut: any = onProgress;
|
||
let onCompleteOut: any = onComplete;
|
||
if (onComplete === undefined) {
|
||
const isValidType = js.isChildClassOf(type as AssetType, Asset);
|
||
if (onProgress) {
|
||
onCompleteOut = onProgress as CompleteCallback;
|
||
if (isValidType) {
|
||
onProgressOut = null;
|
||
}
|
||
} else if (onProgress === undefined && !isValidType) {
|
||
onCompleteOut = type as CompleteCallback;
|
||
onProgressOut = null;
|
||
typeOut = null;
|
||
}
|
||
if (onProgress !== undefined && !isValidType) {
|
||
onProgressOut = type as ProgressCallback;
|
||
typeOut = null;
|
||
}
|
||
}
|
||
return { paths: pathsOut, type: typeOut, onProgress: onProgressOut, onComplete: onCompleteOut };
|
||
}
|
||
|
||
private loadByBundleAndArgs<T extends Asset>(bundle: AssetManager.Bundle, args: ILoadResArgs<T>): void {
|
||
if (args.dir) {
|
||
bundle.loadDir(args.paths as string, args.type, args.onProgress, args.onComplete);
|
||
} else {
|
||
if (typeof args.paths == 'string') {
|
||
bundle.load(args.paths, args.type, args.onProgress, args.onComplete);
|
||
} else {
|
||
bundle.load(args.paths, args.type, args.onProgress, args.onComplete);
|
||
}
|
||
}
|
||
}
|
||
|
||
private loadByArgs<T extends Asset>(args: ILoadResArgs<T>) {
|
||
if (args.bundle) {
|
||
if (assetManager.bundles.has(args.bundle)) {
|
||
let bundle = assetManager.bundles.get(args.bundle);
|
||
this.loadByBundleAndArgs(bundle!, args);
|
||
} else {
|
||
// 自动加载bundle
|
||
assetManager.loadBundle(args.bundle, (err, bundle) => {
|
||
if (!err) {
|
||
this.loadByBundleAndArgs(bundle, args);
|
||
}
|
||
})
|
||
}
|
||
} else {
|
||
this.loadByBundleAndArgs(resources, args);
|
||
}
|
||
}
|
||
|
||
public load<T extends Asset>(bundleName: string, paths: string | string[], type: AssetType<T> | null, onProgress: ProgressCallback | null, onComplete: CompleteCallback<T> | null): void;
|
||
public load<T extends Asset>(bundleName: string, paths: string | string[], onProgress: ProgressCallback | null, onComplete: CompleteCallback<T> | null): void;
|
||
public load<T extends Asset>(bundleName: string, paths: string | string[], onComplete?: CompleteCallback<T> | null): void;
|
||
public load<T extends Asset>(bundleName: string, paths: string | string[], type: AssetType<T> | null, onComplete?: CompleteCallback<T> | null): void;
|
||
public load<T extends Asset>(paths: string | string[], type: AssetType<T> | null, onProgress: ProgressCallback | null, onComplete: CompleteCallback<T> | null): void;
|
||
public load<T extends Asset>(paths: string | string[], onProgress: ProgressCallback | null, onComplete: CompleteCallback<T> | null): void;
|
||
public load<T extends Asset>(paths: string | string[], onComplete?: CompleteCallback<T> | null): void;
|
||
public load<T extends Asset>(paths: string | string[], type: AssetType<T> | null, onComplete?: CompleteCallback<T> | null): void;
|
||
public load<T extends Asset>(
|
||
bundleName: string,
|
||
paths?: string | string[] | AssetType<T> | ProgressCallback | CompleteCallback | null,
|
||
type?: AssetType<T> | ProgressCallback | CompleteCallback | null,
|
||
onProgress?: ProgressCallback | CompleteCallback | null,
|
||
onComplete?: CompleteCallback | null,
|
||
) {
|
||
let args: ILoadResArgs<T> | null = null;
|
||
if (typeof paths === "string" || paths instanceof Array) {
|
||
args = this.parseLoadResArgs(paths, type, onProgress, onComplete);
|
||
args.bundle = bundleName;
|
||
} else {
|
||
args = this.parseLoadResArgs(bundleName, paths, type, onProgress);
|
||
}
|
||
this.loadByArgs(args);
|
||
}
|
||
|
||
public loadDir<T extends Asset>(bundleName: string, dir: string, type: AssetType<T> | null, onProgress: ProgressCallback | null, onComplete: CompleteCallback<T[]> | null): void;
|
||
public loadDir<T extends Asset>(bundleName: string, dir: string, onProgress: ProgressCallback | null, onComplete: CompleteCallback<T[]> | null): void;
|
||
public loadDir<T extends Asset>(bundleName: string, dir: string, onComplete?: CompleteCallback<T[]> | null): void;
|
||
public loadDir<T extends Asset>(bundleName: string, dir: string, type: AssetType<T> | null, onComplete?: CompleteCallback<T[]> | null): void;
|
||
public loadDir<T extends Asset>(dir: string, type: AssetType<T> | null, onProgress: ProgressCallback | null, onComplete: CompleteCallback<T[]> | null): void;
|
||
public loadDir<T extends Asset>(dir: string, onProgress: ProgressCallback | null, onComplete: CompleteCallback<T[]> | null): void;
|
||
public loadDir<T extends Asset>(dir: string, onComplete?: CompleteCallback<T[]> | null): void;
|
||
public loadDir<T extends Asset>(dir: string, type: AssetType<T> | null, onComplete?: CompleteCallback<T[]> | null): void;
|
||
public loadDir<T extends Asset>(
|
||
bundleName: string,
|
||
dir?: string | AssetType<T> | ProgressCallback | CompleteCallback | null,
|
||
type?: AssetType<T> | ProgressCallback | CompleteCallback | null,
|
||
onProgress?: ProgressCallback | CompleteCallback | null,
|
||
onComplete?: CompleteCallback | null,
|
||
) {
|
||
let args: ILoadResArgs<T> | null = null;
|
||
if (typeof dir === "string") {
|
||
args = this.parseLoadResArgs(dir, type, onProgress, onComplete);
|
||
args.bundle = bundleName;
|
||
} else {
|
||
args = this.parseLoadResArgs(bundleName, dir, type, onProgress);
|
||
}
|
||
args.dir = args.paths as string;
|
||
this.loadByArgs(args);
|
||
}
|
||
|
||
public loadRemote<T extends Asset>(url: string, options: IRemoteOptions | null, onComplete?: CompleteCallback<T> | null): void;
|
||
public loadRemote<T extends Asset>(url: string, onComplete?: CompleteCallback<T> | null): void;
|
||
public loadRemote(url: string, ...args: any): void {
|
||
assetManager.loadRemote(url, args);
|
||
}
|
||
}
|
||
|
||
export let resLoader = new ResLoader(); |