Explorar el Código

[FC]:重写LoadResUtil脚本 更新为新API以及使用泛型

fengcong hace 5 años
padre
commit
0e7d658007
Se han modificado 1 ficheros con 152 adiciones y 69 borrados
  1. 152 69
      mk_framework/assets/script/mk/utils/LoadResUtil.ts

+ 152 - 69
mk_framework/assets/script/mk/utils/LoadResUtil.ts

@@ -1,9 +1,10 @@
+
 /**加载资源工具类
 /**加载资源工具类
  * @description 
  * @description 
  * @author 冯聪
  * @author 冯聪
  */
  */
 export default class LoadResUtil {
 export default class LoadResUtil {
-    
+
     /**
     /**
      * 日志标签
      * 日志标签
      * @description 
      * @description 
@@ -11,96 +12,178 @@ export default class LoadResUtil {
     private static _logTag = "[LoadResUtil]";
     private static _logTag = "[LoadResUtil]";
 
 
     /**
     /**
-     * 加载本地图片资源
-     * @description
-     * @param          url           资源加载路径
-     * @returns                      类型 Promise<any> 
+     * 预加载本地资源 
+     * @description https://docs.cocos.com/creator/manual/zh/release-notes/subpackage-upgrade-guide.html
+     * @param paths 加载路径
+     * @param type  资源类型(cc.Asset)
+     * @returns Promise
      */
      */
-    public static loadLoaclSprite(url: string) {
-        return new Promise((resolve, reject) => {
-            cc.loader.loadRes(`${url}`, cc.SpriteFrame, (err, spr) => {
-                if (err) {
-                    reject(err);
-                }
-                resolve(spr);
-            })
+    public preload(paths: string | any, type: typeof cc.Asset): Promise<any> {
+        return new Promise<cc.AssetManager.RequestItem[]>((resolve: (data: cc.AssetManager.RequestItem[]) => void, reject) => {
+            cc.resources.preload(paths, type,
+                //onProgress
+                (finish: number, total: number, item: cc.AssetManager.RequestItem) => {
+
+                },
+                //onComplete
+                (err: Error = null, data: cc.AssetManager.RequestItem[]): void => {
+                    if (!err) {
+                        resolve(data);
+                    }
+                    else {
+                        reject(err);
+                    }
+                });
         });
         });
     }
     }
 
 
     /**
     /**
-     * 加载远程图片资源
-     * @description
-     * @param          url           资源加载路径
-     * @returns                      类型 Promise<any> 
+     * 加载本地资源 
+     * @description https://docs.cocos.com/creator/manual/zh/release-notes/subpackage-upgrade-guide.html
+     * @param paths 加载路径
+     * @param type  资源类型(cc.Asset)
+     * @returns Promise
      */
      */
-    public static loadRemoteSprite(url: string) {
-        return new Promise((resolve, reject) => {
-            cc.loader.load(`${url}`, (err, res) => {
-                if (err) {
-                    reject(err);
-                }
-                let tex: cc.Texture2D = res as cc.Texture2D;
-                let spriteFrame = new cc.SpriteFrame(tex);
-                resolve(spriteFrame);
-            })
+    public static load<T extends cc.Asset>(paths: string | any, type: typeof cc.Asset): Promise<any> {
+        return new Promise<T>((resolve: (data: T) => void, reject) => {
+            cc.resources.load(paths, type,
+                //onProgress
+                (finish: number, total: number, item: cc.AssetManager.RequestItem) => {
+
+                },
+                //onComplete
+                (err: Error = null, data: T): void => {
+                    if (!err) {
+                        resolve(data);
+                    }
+                    else {
+                        reject(err);
+                    }
+                });
         });
         });
+    }
 
 
+    /**
+     * 预加载文件夹资源 
+     * @description https://docs.cocos.com/creator/manual/zh/release-notes/subpackage-upgrade-guide.html
+     * @param dir   文件夹路径
+     * @param type  资源类型(cc.Asset)
+     * @returns Promise
+     */
+    public preloadDir(dir: string, type: typeof cc.Asset): Promise<any> {
+        return new Promise<cc.AssetManager.RequestItem[]>((resolve: (data: cc.AssetManager.RequestItem[]) => void, reject) => {
+            cc.resources.preloadDir(dir, type,  //onProgress
+                (finish: number, total: number, item: cc.AssetManager.RequestItem) => {
+
+                },
+                (err: Error = null, data: cc.AssetManager.RequestItem[]): void => {
+                    if (!err) {
+                        resolve(data);
+                    }
+                    else {
+                        reject(err);
+                    }
+                });
+        });
     }
     }
 
 
     /**
     /**
-     * 加载预制体
-     * @description
-     * @param          url           资源加载路径
-     * @returns                      类型 Promise<any> 
+     * 加载文件夹资源 
+     * @description https://docs.cocos.com/creator/manual/zh/release-notes/subpackage-upgrade-guide.html
+     * @param dir   文件夹路径
+     * @param type  资源类型(cc.Asset)
+     * @returns Promise
      */
      */
-    public static loadPrefab(url: string): Promise<any> {
-        return new Promise((resolve, reject) => {
-            cc.loader.loadRes(`${url}`, function (err, prefab: cc.Prefab) {
-                if (err) {
-                    reject(err);
-                }
-                else {
-                    resolve(prefab);
-                }
-            }.bind(this));
+    public loadDir<T extends cc.Asset>(dir: string, type: typeof cc.Asset): Promise<any> {
+        return new Promise<T>((resolve: (data: T) => void, reject) => {
+            cc.resources.loadDir(dir, type,
+                //onProgress
+                (finish: number, total: number, item: cc.AssetManager.RequestItem) => {
+
+                },
+                //onComplete
+                (err: Error = null, data: T | any): void => {
+                    if (!err) {
+                        resolve(data);
+                    }
+                    else {
+                        reject(err);
+                    }
+                });
+        });
+    }
+
+    /**
+     * 加载远程资源 
+     * @description https://docs.cocos.com/creator/manual/zh/release-notes/subpackage-upgrade-guide.html
+     * @param url   加载路径
+     * @param options  
+     * @returns Promise
+     */
+    public loadRemote<T extends cc.Asset>(url: string, options: Record<string, any>): Promise<any> {
+        return new Promise<T>((resolve: (data: T) => void, reject) => {
+            cc.assetManager.loadRemote(url, options,
+                //onComplete
+                (err: Error = null, data: T): void => {
+                    if (!err) {
+                        resolve(data);
+                    }
+                    else {
+                        reject(err);
+                    }
+                });
         });
         });
     }
     }
 
 
     /**
     /**
-     * 加载JSON文件
-     * @description
-     * @param          url           资源加载路径
-     * @returns                      类型 Promise<any> 
+     * 预加载场景 
+     * @description https://docs.cocos.com/creator/manual/zh/release-notes/subpackage-upgrade-guide.html
+     * @param url   加载路径
+     * @param options  
+     * @returns Promise
      */
      */
-    public static loadJson(url: string): Promise<any> {
-        return new Promise((resolve, reject) => {
-            cc.loader.loadRes(`${url}`, function (err, jsonAsset: cc.JsonAsset) {
-                if (err) {
-                    reject(err);
-                }
-                else {
-                    resolve(jsonAsset.json);
-                }
-            }.bind(this));
+    public preloadScene(sceneName: string, options: Record<string, any>) {
+        return new Promise<void>((resolve: () => void, reject) => {
+            cc.resources.preloadScene(sceneName, options, 
+                //onProgress
+                (finish: number, total: number, item: cc.AssetManager.RequestItem) => {
+
+                },
+                //onComplete
+                (err: Error = null): void => {
+                    if (!err) {
+                        resolve();
+                    }
+                    else {
+                        reject(err);
+                    }
+                });
         });
         });
     }
     }
 
 
     /**
     /**
-     * 加载音频文件
-     * @description
-     * @param          url           资源加载路径
-     * @returns                      类型 Promise<any> 
+     * 加载场景 
+     * @description https://docs.cocos.com/creator/manual/zh/release-notes/subpackage-upgrade-guide.html
+     * @param url   加载路径
+     * @param options 
+     * @returns Promise
      */
      */
-    public static loadAudioClip(url: string): Promise<any> {
-        return new Promise((resolve, reject) => {
-            cc.loader.loadRes(`${url}`, function (err, clip: cc.AudioClip) {
-                if (err) {
-                    reject(err);
-                }
-                else {
-                    resolve(clip);
-                }
-            }.bind(this));
+    public loadScene(sceneName: string, options: Record<string, any>): Promise<any> {
+        return new Promise<any>((resolve: (data: any) => void, reject) => {
+            cc.resources.loadScene(sceneName, options,
+                //onProgress
+                (finish: number, total: number, item: cc.AssetManager.RequestItem) => {
+
+                },
+                //onComplete
+                (err: Error = null, data: cc.SceneAsset): void => {
+                    if (!err) {
+                        resolve(data);
+                    }
+                    else {
+                        reject(err);
+                    }
+                });
         });
         });
     }
     }
 }
 }