1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- import EventManager from "../Event/EventManager";
- import { EventName } from "../Event/EventName";
- import Tool from "../Tool/Tool";
- /**
- * Json资源 - 数据层
- */
- export default class JsonResources {
- /**资源列表 */
- private static resources: Map<string, any> = new Map<string, any>();
- /**加载Json资源 */
- public static loadJsonResources (resPath: string, list: Array<string>, denominator: number, currentProgress: number, callback: Function) {
- Tool.log("");
- Tool.log("加载Json资源");
- cc.assetManager.loadBundle(((cc.sys.platform == cc.sys.DESKTOP_BROWSER)? "":resPath)+"res/remoteJson", (err: Error, prefab: cc.AssetManager.Bundle) => {
- let finish = 0;
- for (let i in list) {
- prefab.load(list[i], cc.JsonAsset, (err: Error, assets: cc.JsonAsset) => {
- Tool.log(err);
- EventManager.sendEvent(EventName.Res.Res_LoadProgress, (finish/list.length)*denominator + currentProgress);
- finish++;
- // 使用map结构保存资源
- this.resources.set(assets.name, assets.json);
- if (finish == list.length) {
- Tool.log(this.resources);
- Tool.log("加载完毕");
- Tool.log("");
-
- if (callback == null) {
- // 发送初始加载完成 本地消息
- EventManager.sendEvent(EventName.Res.Res_LoadComplete);
- }
- else {
- callback();
- }
- }
- });
- }
- });
- }
- /**获得Json资源 */
- public static getResources (name: string): any {
- let _name: string = name;
- if (name.includes("/") == true) {
- _name = name.substring(name.indexOf("/")+1);
- }
- return this.resources.get(_name) || (cc.resources.get(name, cc.JsonAsset) as cc.JsonAsset).json;
- }
- }
|