import { Asset, AssetManager, assetManager, Constructor, error, js, resources, __private} from "cc"; export type ProgressCallback = ((finished: number, total: number, item: AssetManager.RequestItem) => void) | null; export type CompleteCallback = ((err: Error | null, data: T) => void) | null; export type CompleteCallbacks = ((err: Error | null, data: T[]) => void) | null; export type IRemoteOptions = { [k: string]: any, ext?: string }; export type AssetType = Constructor; interface ILoadResArgs { bundle?: string; dir?: string; paths: string; type: AssetType | null; onProgress: ProgressCallback | null; onComplete: CompleteCallback | null; onCompletes: CompleteCallbacks | null; } export default class ResLoader { /** * 加载资源包 * @param url 资源地址 * @param complete 完成事件 * @param v 资源MD5版本号 */ loadBundle(url: string, v?: string) { return new Promise((resolve, reject) => { assetManager.loadBundle(url, { version: v }, (err, bundle: AssetManager.Bundle) => { if (err) { return error(err); } resolve(bundle); }); }); } parseLoadResArgs( paths: string | string[], type?: AssetType | ProgressCallback | CompleteCallback |CompleteCallbacks| null, onProgress?: AssetType | ProgressCallback | CompleteCallback | CompleteCallbacks|null, onComplete?: ProgressCallback | CompleteCallback | CompleteCallbacks|null ) { let pathsOut: any = paths; let typeOut: any = type; let onProgressOut: any = onProgress; let onCompleteOut: any = onComplete; let onCompleteOuts: any = null; 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; if(onCompleteOut){ onCompleteOuts = null; } else { onCompleteOuts = type as CompleteCallbacks; } onProgressOut = null; typeOut = null; } if (onProgress !== undefined && !isValidType) { onProgressOut = type as ProgressCallback; typeOut = null; } } else{ onCompleteOut = type as CompleteCallback; if(onCompleteOut){ onCompleteOuts = null; } else { onCompleteOuts = type as CompleteCallbacks; } } return { paths: pathsOut, type: typeOut, onProgress: onProgressOut, onComplete: onCompleteOut,onCompletes: onCompleteOuts }; } private loadByBundleAndArgs(bundle: AssetManager.Bundle, args: ILoadResArgs): void { if (args.dir) { bundle.loadDir(args.paths as string, args.type, args.onProgress, args.onCompletes); } 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.onCompletes); } } } private loadByArgs(args: ILoadResArgs) { 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); } } preload(bundleName: string, url: string, type: typeof Asset) { if (bundleName) { if (assetManager.bundles.has(bundleName)) { let bundle = assetManager.bundles.get(bundleName); bundle.preload(url, type); } else { assetManager.loadBundle(bundleName, (error: Error, bundle: AssetManager.Bundle) => { if (!error) { bundle.preload(url, type); } }) } } else { resources.preload(url, type); } } load(bundleName: string, paths: string | string[], type: AssetType | null, onProgress: ProgressCallback | null, onComplete: CompleteCallback | null): void; load(bundleName: string, paths: string | string[], onProgress: ProgressCallback | null, onComplete: CompleteCallback | null): void; load(bundleName: string, paths: string | string[], onComplete?: CompleteCallback | null): void; load(bundleName: string, paths: string | string[], type: AssetType | null, onComplete?: CompleteCallback | null): void; load(paths: string | string[], type: AssetType | null, onProgress: ProgressCallback | null, onComplete: CompleteCallback | null): void; load(paths: string | string[], onProgress: ProgressCallback | null, onComplete: CompleteCallback | null): void; load(paths: string | string[], onComplete?: CompleteCallback | null): void; load(paths: string | string[], type: AssetType | null, onComplete?: CompleteCallback | null): void; load( bundleName: string, paths?: string | string[] | AssetType | ProgressCallback | CompleteCallback | null, type?: AssetType | ProgressCallback | CompleteCallback | null, onProgress?: ProgressCallback | CompleteCallback | null, onComplete?: CompleteCallback | null, ) { let args: ILoadResArgs | 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); } loadDir(bundleName: string, dir: string, type: AssetType | null, onProgress: ProgressCallback | null, onComplete: CompleteCallback | null): void; loadDir(bundleName: string, dir: string, onProgress: ProgressCallback | null, onComplete: CompleteCallback | null): void; loadDir(bundleName: string, dir: string, onComplete?: CompleteCallback | null): void; loadDir(bundleName: string, dir: string, type: AssetType | null, onComplete?: CompleteCallback | null): void; loadDir(dir: string, type: AssetType | null, onProgress: ProgressCallback | null, onComplete: CompleteCallback | null): void; loadDir(dir: string, onProgress: ProgressCallback | null, onComplete: CompleteCallback | null): void; loadDir(dir: string, onComplete?: CompleteCallback | null): void; loadDir(dir: string, type: AssetType | null, onComplete?: CompleteCallback | null): void; loadDir( bundleName: string, dir?: string | AssetType | ProgressCallback | CompleteCallback | null, type?: AssetType | ProgressCallback | CompleteCallback | null, onProgress?: ProgressCallback | CompleteCallback | null, onComplete?: CompleteCallback | null, ) { let args: ILoadResArgs | 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); } loadRemote(url: string, options: IRemoteOptions | null, onComplete?: CompleteCallback | null): void; loadRemote(url: string, onComplete?: CompleteCallback | null): void; loadRemote(url: string, ...args: any): void { assetManager.loadRemote(url, args); } release(path: string, bundleName: string = "resources") { let bundle = assetManager.getBundle(bundleName); bundle?.release(path); } releaseDir(path: string, bundleName: string = "resources") { let bundle: AssetManager.Bundle | null = assetManager.getBundle(bundleName); let infos = bundle?.getDirWithPath(path); infos?.map(function (info) { let asset = assetManager.assets.get(info.uuid)!; assetManager.releaseAsset(asset); }); if (path == "" && bundleName != "resources" && bundle) { assetManager.removeBundle(bundle); } } get(path: string, type?: __private.__types_globals__Constructor | null, bundleName: string = "resources"): T | null { let bundle: AssetManager.Bundle | null = assetManager.getBundle(bundleName); return bundle!.get(path, type); } dump() { assetManager.assets.forEach((value: Asset, key: string) => { console.log(key); }) console.log(`当前资源总数:${assetManager.assets.count}`); } } export let resLoader = new ResLoader();