import { Asset, assetManager, AssetManager, Component, Node, resources, SceneAsset, __private } from "cc"; /** * 资源工具,resources的api全部await化 * @author 袁浩 */ export class ResourcesUtils { /** * 加载本地资源,使用说明见resources.load * @returns await 加载成功后返回的数据 */ public static load(paths: string | any, type: __private.cocos_core_asset_manager_shared_AssetType, component: Component | Node, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) { return new Promise((resolve: (data: T) => void, reject) => { resources.load(paths, type, onProgress, (err: Error = null, data: T): void => { if (!err) { if (component && !component.isValid) { return; } resolve(data); } else { reject(err); } }); }); } /** * 加载远程资源,使用说明见assetManager.loadRemote * @returns await 加载成功后返回的数据 */ public static loadRemote(url: string, options: __private.cocos_core_asset_manager_shared_IRemoteOptions, component: Component, 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 (component && !component.isValid) { return; } resolve(data); } else { reject(err); } }); }); } /** * 使用说明见resources.preload * @returns await 加载成功后返回的数据 */ public static 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 static 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 static 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 static 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); } }); }); } }