| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 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<T extends Asset>(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<T>((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<T extends Asset>(url: string, options: __private.cocos_core_asset_manager_shared_IRemoteOptions, component: Component, onComplete?: __private.cocos_core_asset_manager_shared_CompleteCallbackWithData<T> | null) {
- return new Promise<T>((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<AssetManager.RequestItem[]>((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<T extends Asset>(dir: string, type: __private.cocos_core_asset_manager_shared_AssetType<T> = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
- return new Promise<T>((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<AssetManager.RequestItem[]>((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<any>((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<void>((resolve: () => void, reject) => {
- resources.preloadScene(sceneName, options, onProgress, (err: Error = null): void => {
- if (!err) {
- resolve();
- }
- else {
- reject(err);
- }
- });
- });
- }
- }
|