|
|
@@ -0,0 +1,254 @@
|
|
|
+/*============================================================================================================*/
|
|
|
+/**
|
|
|
+ * @description 资源加载
|
|
|
+ */
|
|
|
+/*============================================================================================================*/
|
|
|
+// import { loader, error, JsonAsset, log, sys, SpriteAtlas, SpriteFrame } from 'cc';
|
|
|
+
|
|
|
+// window.xx = window.xx || {};
|
|
|
+// export class LoaderMgr {
|
|
|
+export class LoaderMgr {
|
|
|
+ // xx.Loader = {
|
|
|
+ private _profile_res = {};
|
|
|
+ private _bundle = null;
|
|
|
+ // todo 根据场景来清除资源缓存(什么场景加载的资源就保存到什么场景资源配置上。
|
|
|
+ // 可能存在的问题:两个场景公用的资源可能会被释放,之后重新加载,可能会有问题
|
|
|
+ // )
|
|
|
+ public setBundle(bundle) {
|
|
|
+ bundle && (this._bundle = bundle);
|
|
|
+ }
|
|
|
+
|
|
|
+ public getBundle() {
|
|
|
+ return this._bundle || cc.resources;
|
|
|
+ }
|
|
|
+ /*============================================================================================================*/
|
|
|
+ /**
|
|
|
+ * 异步加载资源
|
|
|
+ */
|
|
|
+ /*============================================================================================================*/
|
|
|
+ public loadAsset(relative_path: string, complete_callback) {
|
|
|
+ let loader = this.getBundle();
|
|
|
+ loader.load(relative_path, (error, asset) => {
|
|
|
+ if (error) {
|
|
|
+ error('xx.loadAsset failed ... and path = ' + relative_path, '===error:', JSON.stringify(error));
|
|
|
+ return;
|
|
|
+ } else {
|
|
|
+ complete_callback && complete_callback(asset);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /*============================================================================================================*/
|
|
|
+ /**
|
|
|
+ * 根据类型异步加载
|
|
|
+ */
|
|
|
+ /*============================================================================================================*/
|
|
|
+ public loadAssetByType(relative_path: string, asset_type, complete_callback?: Function) {
|
|
|
+ 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 type 资源类型,如果为空,则返回所有类型文件地址
|
|
|
+ */
|
|
|
+ /*============================================================================================================*/
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*============================================================================================================*/
|
|
|
+ /**
|
|
|
+ * 通过config构造uuid数组加载
|
|
|
+ */
|
|
|
+ /*============================================================================================================*/
|
|
|
+ public loadByConfigUseUuids(assets_config, progress_callback, complete_callback) {
|
|
|
+ // let uuids = [];
|
|
|
+ // let urls = [];
|
|
|
+ // let len = assets_config.length;
|
|
|
+ // for (let i = 0; i < len; i++) {
|
|
|
+ // let config = assets_config[i];
|
|
|
+ // let path = config.path;
|
|
|
+ // let type = config.type;
|
|
|
+ // let _uuids = this.getBundle().getDirWithPath(path, type, urls);
|
|
|
+ // uuids = uuids.concat(_uuids);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // if (uuids.length === 0) return;
|
|
|
+
|
|
|
+ // let DoWithAssetItemOnProgress = function (completed_count, total_count, item) {
|
|
|
+ // let asset = item.content;
|
|
|
+ // if (asset instanceof JsonAsset) {
|
|
|
+ // xx.Dict.loadByAsset(asset);
|
|
|
+ // }
|
|
|
+ // progress_callback && progress_callback(completed_count, total_count);
|
|
|
+ // };
|
|
|
+
|
|
|
+ // let DoWithAssetOnComplete = function (errors, res_assets, res_urls) {
|
|
|
+ // complete_callback && complete_callback(errors, res_assets, res_urls);
|
|
|
+ // };
|
|
|
+
|
|
|
+ // loader._loadResUuids(uuids, DoWithAssetItemOnProgress, DoWithAssetOnComplete, urls);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /*============================================================================================================*/
|
|
|
+ /**
|
|
|
+ */
|
|
|
+ /*============================================================================================================*/
|
|
|
+ public releaseResByPathAndType(path = '', type) {
|
|
|
+ if (!path || !type) return;
|
|
|
+
|
|
|
+ let urls = this.getDirUrlsRecursion(path, type);
|
|
|
+ let len = urls.length;
|
|
|
+ for (let i = 0; i < len; ++i) {
|
|
|
+ let url = urls[i];
|
|
|
+ let atlas = this.getBundle().getRes(url, type);
|
|
|
+ let deps = this.getBundle().getDependsRecursively(atlas);
|
|
|
+ // assetManager.releaseAsset(deps);
|
|
|
+ }
|
|
|
+ cc.sys.garbageCollect();
|
|
|
+ }
|
|
|
+
|
|
|
+ /*============================================================================================================*/
|
|
|
+ /**
|
|
|
+ * 同步获取已经预加载的图集中的spriteframe 此接口进入游戏后生效
|
|
|
+ */
|
|
|
+ /*============================================================================================================*/
|
|
|
+ public getSpriteFrameFromAtlas(sprite_frame_name, path = '/Atlas') {
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /*============================================================================================================*/
|
|
|
+ /**
|
|
|
+ * @description 获取散图资源(需要在loading的时候首先加载)
|
|
|
+ * @param {string} sprite_frame_path 资源相对于resource下的路径
|
|
|
+ */
|
|
|
+ /*============================================================================================================*/
|
|
|
+ public getSpriteFrame(sprite_frame_path) {
|
|
|
+ let res = this.getBundle().getRes(sprite_frame_path, cc.SpriteFrame);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*============================================================================================================*/
|
|
|
+ /**
|
|
|
+ * SpriteFrame有一个_uuid的属性,根据uuid去loader里面查找路径加载路径
|
|
|
+ */
|
|
|
+ /*============================================================================================================*/
|
|
|
+ public parseUuidToUrl(uuid) {
|
|
|
+ let path_to_uuid = this.getBundle()._pathToUuid;
|
|
|
+ for (let key in path_to_uuid) {
|
|
|
+ let entrys = path_to_uuid[key];
|
|
|
+ if (entrys instanceof Array) {
|
|
|
+ for (let entry of entrys) {
|
|
|
+ if (entry.uuid === uuid) {
|
|
|
+ return key;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (entrys.uuid === uuid) {
|
|
|
+ return key;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+
|
|
|
+ public loadRemoteTexture(url, role_id, callback) {
|
|
|
+ if (this._profile_res[role_id]) {
|
|
|
+ let spf = this._profile_res[role_id];
|
|
|
+ callback && callback(role_id, spf);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!url) return;
|
|
|
+
|
|
|
+ if (!cc.sys.isNative) return;
|
|
|
+
|
|
|
+ this.getBundle().load({ url: url, type: 'png' }, (err, tex) => {
|
|
|
+ cc.log('===get_texture:', role_id);
|
|
|
+ if (err) {
|
|
|
+ cc.log('===err:', err);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ let spf = new tex();//SpriteFrame(tex)
|
|
|
+ this._profile_res[role_id] = spf;
|
|
|
+ callback && callback(role_id, spf);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ /** 获取图片纹理 by 图集
|
|
|
+ * - sprite_frame_name:要获取的纹理名字
|
|
|
+ * - arr_atlas:图集列表
|
|
|
+ */
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+};
|
|
|
+// declare global {
|
|
|
+// const LoaderMgr: LoaderMgr;
|
|
|
+// }
|
|
|
+// // window['LoaderMgr'] = new LoaderMgr();
|
|
|
+// mk.loader = new LoaderMgr();
|