import { _decorator, Component, __private, AssetManager, resources, Asset, assetManager, SceneAsset } from "cc"; const { ccclass, property } = _decorator; /** * 资源加载组件 * @author 袁浩 */ @ccclass('ResourceLoader') export class ResourceLoader extends Component { /** * 加载本地资源,使用说明见resources.load * @returns await 加载成功后返回的数据 */ public load(paths: string[] | string, type: __private.cocos_core_asset_manager_shared_AssetType, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) { return new Promise((resolve: (data: T) => void, reject) => { resources.load(paths as any, type, onProgress, (err: Error = null, data: T): void => { if (!err) { if (!this.node || !this.node.isValid) { return; } resolve(data); } else { reject(err); } }); }); } /** * 加载远程资源,使用说明见assetManager.loadRemote * @returns await 加载成功后返回的数据 */ public loadRemote(url: string, options: __private.cocos_core_asset_manager_shared_IRemoteOptions, onComplete?: __private.cocos_core_asset_manager_shared_CompleteCallbackWithData | null) { return new Promise((resolve: (data: T) => void, reject) => { assetManager.loadRemote(url, options, (err: Error = null, data: T): void => { if (!err) { if (!this.node.isValid) { return; } resolve(data); } else { reject(err); } }); }); } /** * 使用说明见resources.preload * @returns await 加载成功后返回的数据 */ public preload(paths: string | any, type: __private.cocos_core_asset_manager_shared_AssetType = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) { return new Promise((resolve: (data: AssetManager.RequestItem[]) => void, reject) => { resources.preload(paths, type, onProgress, (err: Error = null, data: AssetManager.RequestItem[]): void => { if (!err) { resolve(data); } else { reject(err); } }); }); } /** * 使用说明见resources.loadDir * @returns await 加载成功后返回的数据 */ public loadDir(dir: string, type: __private.cocos_core_asset_manager_shared_AssetType = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) { return new Promise((resolve: (data: T) => void, reject) => { resources.loadDir(dir, type, onProgress, (err: Error = null, data: T | any): void => { if (!err) { resolve(data); } else { reject(err); } }); }); } /** * 使用说明见resources.preloadDir * @returns await 加载成功后返回的数据 */ public preloadDir(dir: string, type: __private.cocos_core_asset_manager_shared_AssetType = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) { return new Promise((resolve: (data: AssetManager.RequestItem[]) => void, reject) => { resources.preloadDir(dir, type, onProgress, (err: Error = null, data: AssetManager.RequestItem[]): void => { if (!err) { resolve(data); } else { reject(err); } }); }); } /** * 使用说明见resources.loadScene * @returns await 加载成功后返回的数据 */ public loadScene(sceneName: string, options: __private.cocos_core_asset_manager_shared_IAssetOptions = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) { return new Promise((resolve: (data: any) => void, reject) => { resources.loadScene(sceneName, options, onProgress, (err: Error = null, data: SceneAsset): void => { if (!err) { resolve(data); } else { reject(err); } }); }); } /** * 使用说明见resources.preloadScene * @returns await 加载成功后返回的数据 */ public preloadScene(sceneName: string, options: __private.cocos_core_asset_manager_shared_IAssetOptions = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback) { return new Promise((resolve: () => void, reject) => { resources.preloadScene(sceneName, options, onProgress, (err: Error = null): void => { if (!err) { resolve(); } else { reject(err); } }); }); } /** * 加载资源包 * @param nameOrUrl 资源包名字或者地址 * @returns 资源包对象 */ public loadBundle(nameOrUrl: string) { return new Promise((resolve: (data: AssetManager.Bundle) => void, reject) => { assetManager.loadBundle(nameOrUrl, (err: Error | null, data: AssetManager.Bundle) => { if (!err) { resolve(data); } else { reject(err); } }); }); } /** * 加载资源包中的资源 * @param bundle * @param paths * @param type * @param onProgress * @returns */ public loadInBundle(bundle: AssetManager.Bundle, paths: string[] | string, type: __private.cocos_core_asset_manager_shared_AssetType, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) { return new Promise((resolve: (data: T) => void, reject) => { bundle.load(paths as any, type, onProgress, (err: Error = null, data: T) => { if (!err) { resolve(data); } else { reject(err); } }); }); } }