Просмотр исходного кода

Merge branch 'master' of http://git.mokasz.com/zouyong/mk_framework

# Conflicts:
#	mk_framework/assets/script/mk/system/MKSystem.ts
zouyong 5 лет назад
Родитель
Сommit
315a85e9cf

+ 2 - 2
mk_framework/assets/script/game/AudioManager.ts

@@ -51,7 +51,7 @@ export default class AudioManager {
     public async playMusic(audio_name: AUDIO_NAME, loop: boolean = true) {
         let asset = this._map_audio.get(audio_name);
         if (!asset) {
-            // asset = await mk.loader.loadAssetByType(AUDIO_PATH + audio_name, cc.AudioClip);
+            asset = await mk.loader.load(AUDIO_PATH + audio_name, cc.AudioClip);
             this._map_audio.set(audio_name, asset);
         }
         mk.audio.playMusic(asset, loop);
@@ -66,7 +66,7 @@ export default class AudioManager {
         if (this._switchEffect && this._switchTempEffect) {
             let asset = this._map_audio.get(audio_name);
             if (!asset) {
-                // asset = await mk.loader.loadAssetByType(AUDIO_PATH + audio_name, cc.AudioClip);
+                asset = await mk.loader.load(AUDIO_PATH + audio_name, cc.AudioClip);
                 this._map_audio.set(audio_name, asset);
             }
             mk.audio.playEffect(asset, loop);

+ 0 - 137
mk_framework/assets/script/mk/system/LoaderSystem.ts

@@ -1,137 +0,0 @@
-/**
- * @description 资源加载
- * @author xhx
- */
-export default class LoaderSystem {
-    private _bundle = null;
-    public setBundle(bundle) {
-        bundle && (this._bundle = bundle);
-    }
-
-    public getBundle() {
-        return this._bundle || cc.resources;
-    }
-    /**
-     * 
-     * @param relative_path 资源路径
-     * @param asset_type 资源类型
-     * @param complete_callback 完成回调【选填】
-     * @returns 对应的资源
-     */
-    public loadAssetByType<T extends typeof cc.Asset>(relative_path: string, asset_type: T, complete_callback?: Function): Promise<InstanceType<T>> {
-        return new Promise((resolve: (data) => void, reject) => {
-            this.getBundle().load(relative_path, asset_type, (error, asset) => {
-                if (error) {
-                    error('xx.loadAssetByType failed ... and path = ' + relative_path, '==error:', JSON.stringify(error));
-                    reject();
-                    return;
-                } else {
-                    complete_callback && complete_callback(asset);
-                    resolve(asset);
-                }
-            });
-        });
-    }
-
-    /**
-     * 获取resource路径下资源地址数组
-     * @param path 文件夹路径
-     * @param type 文件类型
-     * @returns [路径]
-     */
-    public getDirUrlsRecursion(path, type) {
-        let urls = [];
-        this.getBundle().getDirWithPath(path, type, urls);
-        let arr_path = [];
-        for (let i = 0; i < urls.length; i++) {
-            const element = urls[i].path;
-            arr_path.push(element);
-        }
-        return arr_path;
-    }
-
-
-    /**
-     * 预加载资源 by 配置表 【暂未使用】
-     * @param assets_config 配置表
-     * @param progress_callback 进度回调
-     * @param complete_callback 完成回调
-     */
-    public loadAssetsByConfig(assets_config, progress_callback, complete_callback) {
-        let total_count = 0;
-        let complete_count = 0;
-        for (let i = 0; i < assets_config.length; i++) {
-            let config = assets_config[i];
-            let path = config.path;
-            let type = config.type;
-
-            let urls = [];
-            this.getBundle().getDirWithPath(path, type, urls);
-            total_count += urls.length;
-            cc.log('预加载资源:%o', urls);
-            let callback = config.cb;
-            for (let i = 0; i < urls.length; i++) {
-                let path = urls[i].path;
-                this.getBundle().load(path, type, function (err, spriteFrame) {
-                    if (err) {
-                        cc.error(err.message || err);
-                        return;
-                    }
-                    ++complete_count;
-                    progress_callback && progress_callback(complete_count, total_count, spriteFrame);
-                    callback && callback(spriteFrame);
-
-                    if (complete_count === total_count) {
-                        complete_callback && complete_callback();
-                    }
-                });
-            }
-        }
-    }
-
-
-    /**
-     * 同步获取已经预加载的图集中的spriteframe 此接口进入游戏后生效
-     * @param sprite_frame_name 要获取的纹理名字
-     * @param path 图集路径
-     * @returns cc.SpriteFrame
-     */
-    public getSpriteFrameFromAtlas(sprite_frame_name, path = '/Atlas'): cc.SpriteFrame {
-        let urls = this.getDirUrlsRecursion(path, cc.SpriteAtlas);
-        let len = urls.length;
-        for (let i = 0; i < len; i++) {
-            let url = urls[i];
-            let atlas: any = this.getBundle().getRes(url, cc.SpriteAtlas);//resources.get
-            if (atlas) {
-                let sf = atlas.getSpriteFrame(sprite_frame_name);
-                if (sf) return sf;
-            }
-        }
-    }
-
-    /**
-     * 获取散图资源(需要在loading的时候首先加载)
-     * @param sprite_frame_path 资源路径
-     * @returns cc.SpriteFrame || null
-     */
-    public getSpriteFrame(sprite_frame_path: string): cc.SpriteFrame {
-        let res = this.getBundle().getRes(sprite_frame_path, cc.SpriteFrame);
-        return res;
-    }
-
-    /**
-     * 获取图片纹理 by 图集 
-     * @param sprite_frame_name 要获取的纹理名字
-     * @param arr_atlas [图集列表]
-     * @returns cc.SpriteFrame || null
-     */
-    public getSpriteFrameByArrAlst(sprite_frame_name: string, arr_atlas: Array<cc.SpriteAtlas>): cc.SpriteFrame {
-        let count = arr_atlas.length;
-        for (let i = 0; i < count; i++) {
-            let _atlas = arr_atlas[i];
-            let spf = _atlas.getSpriteFrame(sprite_frame_name);
-            if (spf) return spf;
-        }
-        return null;
-    }
-};

+ 0 - 9
mk_framework/assets/script/mk/system/LoaderSystem.ts.meta

@@ -1,9 +0,0 @@
-{
-  "ver": "1.0.8",
-  "uuid": "803afd6a-cc27-421e-8d5b-f88683cb9e0e",
-  "isPlugin": false,
-  "loadPluginInWeb": true,
-  "loadPluginInNative": true,
-  "loadPluginInEditor": false,
-  "subMetas": {}
-}

+ 7 - 3
mk_framework/assets/script/mk/system/MKSystem.ts

@@ -4,7 +4,7 @@ import TimeUtil from "../utils/TimeUtil";
 import AudioSystem from "./AudioSystem";
 import DataSystem from "./DataSystem";
 import HttpSystem from "./HttpSystem";
-import LoaderSystem from "../utils/LoadResUtil";
+import LoadResUtil from "../utils/LoadResUtil";
 import { EncryptUtil } from "../utils/EncryptUtil";
 
 
@@ -16,14 +16,18 @@ class MKSystem {
     /** 工具 */
     time: TimeUtil;
     file: FileUtil;
+<<<<<<< .mine
     encrypt:EncryptUtil;
+=======
+    loadRes:LoadResUtil;
+>>>>>>> .theirs
 
     /** SDK */
     bugly:BuglySDK;
     
 
     /** 系统管理 */
-    loader: LoaderSystem;
+    loader: LoadResUtil;
     data:DataSystem;
     http:HttpSystem;
     audio:AudioSystem;
@@ -32,13 +36,13 @@ class MKSystem {
         //工具
         this.time = new TimeUtil();
         this.file = new FileUtil();
+        this.loader = new LoadResUtil();
         this.encrypt = new EncryptUtil();
 
         //sdk
         this.bugly = new BuglySDK();
 
         //system
-        this.loader = new LoaderSystem();
         this.audio = new AudioSystem();
         this.data = new DataSystem();
         this.http = new HttpSystem();

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

@@ -1,9 +1,10 @@
+
 /**加载资源工具类
  * @description 
  * @author 冯聪
  */
 export default class LoadResUtil {
-    
+
     /**
      * 日志标签
      * @description 
@@ -11,96 +12,178 @@ export default class 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 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);
+                    }
+                });
         });
     }
 }

+ 98 - 28
mk_framework/assets/script/mk/utils/LogUtil.ts

@@ -5,56 +5,88 @@
 export default class LogUtil {
 
     /**
+     * 是否显示Log
+     * @description
+     */
+    public ifShowLog: boolean = true;
+
+    /**
+     * 标签符号
+     * @description 标签与打印内容之间的拼接符号
+     */
+    private _tagChar: string = "...";
+
+    /**
      * 标签符号
      * @description 标签与打印内容之间的拼接符号
      */
-    private static _tagChar: string = ": ";
+    private _frontChar: string = "    ";
 
     /**
      * 对象key&value间隔符号
      * @description 打印对象时,key与value之间的拼接符号
      */
-    private static _keyValueChar: string = " = ";
+    private _keyValueChar: string = " = ";
 
     /**
      * 打印普通日志
      * @description
-     * @param          tag           日志标签(用于标识日志)
-     * @param          data          日志内容
+     * @param  tag  日志标签(用于标识日志)
+     * @param  data 日志内容
      */
-    public static log(tag: string, ...data: any[]) {
-        console.log(tag, this._tagChar, ...data);
+    public log(tag: string, ...data: any[]) {
+        if (!this.ifShowLog) {
+            return;
+        }
+        let totalTag = tag + this._tagChar;
+        let message = this.getMessage(data);
+        console.log("%c " + totalTag, "color: " + LogTagColor.bule, message);
     }
 
     /**
      * 打印警告日志
      * @description
-     * @param          tag            日志标签(用于标识日志)
-     * @param          data           日志内容
+     * @param  tag   日志标签(用于标识日志)
+     * @param  data  日志内容
      */
-    public static warn(tag: string, ...data: any[]) {
-        console.warn(tag, this._tagChar, ...data);
+    public warn(tag: string, ...data: any[]) {
+        if (!this.ifShowLog) {
+            return;
+        }
+        let totalTag = tag + this._tagChar;
+        let message = this.getMessage(data);
+        console.warn("%c " + totalTag, "color: " + LogTagColor.orange, message);
     }
 
     /**
      * 打印错误日志
      * @description
-     * @param          tag           日志标签(用于标识日志)
-     * @param          data          日志内容
+     * @param  tag   日志标签(用于标识日志)
+     * @param  data  日志内容
      */
-    public static error(tag: string, ...data: any[]) {
-        console.error(tag, this._tagChar, ...data);
+    public error(tag: string, ...data: any[]) {
+        if (!this.ifShowLog) {
+            return;
+        }
+        let totalTag = tag + this._tagChar;
+        let message = this.getMessage(data);
+        console.warn("%c " + totalTag, "color: " + LogTagColor.red, message);
     }
 
     /**
      * 打印单个对象日志
-     * @description                   逐个分层打印所有对象的key,value值
-     * @param           tag           日志标签(用于标识日志)
-     * @param           obj           日志对象
-     * @param           frontStrNum   前缀字符数量(不用填)
+     * @description        逐个分层打印所有对象的key,value值
+     * @param tag          日志标签(用于标识日志)
+     * @param obj          日志对象
+     * @param frontStrNum  前缀字符数量(不用填)
      * @returns 
      */
-    public static logSingle(tag: string, obj: any, frontStrNum: number = 0) {
+    public logSingle(tag: string, obj: any, frontStrNum: number = 0) {
+
+        if (!this.ifShowLog) {
+            return;
+        }
+
         if (obj == null || obj == undefined) {
             return
         }
@@ -62,13 +94,20 @@ export default class LogUtil {
         if (typeof obj === "number" || typeof obj === "string" || typeof obj === "boolean") {
             this.log(`${this.getFrontStr(frontStrNum)}${tag}`, obj);
         } else {
-            for (const key of Object.keys(obj)) {
+            let objKeys = Object.keys(obj);
+            for (const key of objKeys) {
                 if (obj.hasOwnProperty(key)) {
+                    let index = objKeys.indexOf(key);
                     if (typeof obj[key] === "object") {
-                        this.log(`${this.getFrontStr(frontStrNum)}${tag}` + key + this._keyValueChar)
-                        this.logSingle(tag, obj[key], ++frontStrNum);
+                        this.log(`${this.getFrontStr(frontStrNum)}`, key, ": ")
+                        this.logSingle(tag, obj[key], frontStrNum + 1)
                     } else {
-                        this.log(`${this.getFrontStr(frontStrNum)}${tag}`, key, this._keyValueChar, obj[key]);
+                        if (!frontStrNum && index === 0) {
+                            this.log(`${this.getFrontStr(frontStrNum)}${tag}\n`, key, this._keyValueChar, obj[key]);
+                        }
+                        else {
+                            this.log(`${this.getFrontStr(frontStrNum)}`, key, this._keyValueChar, obj[key]);
+                        }
                     }
                 }
             }
@@ -76,19 +115,50 @@ export default class LogUtil {
     }
 
     /**
+     * 把日志内容拼接成字符串
+     * @param data 
+     * @returns 
+     */
+    public getMessage(...data: any[]) {
+        let message = "";
+        for (var i = 0; i < data.length; i++) {
+            let str = data[i];
+            let str_new = "";
+            //屏蔽逗号
+            for (var j = 0; j < str.length; j++) {
+                let char: string = str[j];
+                str_new += char;
+            }
+            message += str_new;
+        }
+        return message;
+    }
+
+    /**
      * 获取前缀字符串
-     * @description                   根据前缀字符串数目生成前缀字符串,用以标记层级
-     * @param           frontStrNum   字符串数量
-     * @returns                       返回前缀字符串(string)
+     * @description         根据前缀字符串数目生成前缀字符串,用以标记层级
+     * @param  frontStrNum  字符串数量
+     * @returns             返回前缀字符串(string)
      */
-    private static getFrontStr(frontStrNum: number): string {
+    public getFrontStr(frontStrNum: number): string {
         let str = "";
         if (frontStrNum > 0) {
             for (let i = 0; i < frontStrNum; i++) {
-                str += "--"
+                str += this._frontChar;
             }
             return str;
         }
         return str;
     }
 }
+
+/**日志字体颜色 */
+export enum LogTagColor {
+    red = "red;font-weight:bold",
+    white = "white;font-weight:bold",
+    yellow = "yellow;font-weight:bold",
+    green = "green;font-weight:bold",
+    bule = "blue;font-weight:bold",
+    orange = "orange;font-weight:bold",
+    black = "black;font-weight:bold"
+}