JsonResources.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import EventManager from "../Event/EventManager";
  2. import { EventName } from "../Event/EventName";
  3. import Tool from "../Tool/Tool";
  4. /**
  5. * Json资源 - 数据层
  6. */
  7. export default class JsonResources {
  8. /**资源列表 */
  9. private static resources: Map<string, any> = new Map<string, any>();
  10. /**加载Json资源 */
  11. public static loadJsonResources (resPath: string, list: Array<string>, denominator: number, currentProgress: number, callback: Function) {
  12. Tool.log("");
  13. Tool.log("加载Json资源");
  14. cc.assetManager.loadBundle(((cc.sys.platform == cc.sys.DESKTOP_BROWSER)? "":resPath)+"res/remoteJson", (err: Error, prefab: cc.AssetManager.Bundle) => {
  15. let finish = 0;
  16. for (let i in list) {
  17. prefab.load(list[i], cc.JsonAsset, (err: Error, assets: cc.JsonAsset) => {
  18. Tool.log(err);
  19. EventManager.sendEvent(EventName.Res.Res_LoadProgress, (finish/list.length)*denominator + currentProgress);
  20. finish++;
  21. // 使用map结构保存资源
  22. this.resources.set(assets.name, assets.json);
  23. if (finish == list.length) {
  24. Tool.log(this.resources);
  25. Tool.log("加载完毕");
  26. Tool.log("");
  27. if (callback == null) {
  28. // 发送初始加载完成 本地消息
  29. EventManager.sendEvent(EventName.Res.Res_LoadComplete);
  30. }
  31. else {
  32. callback();
  33. }
  34. }
  35. });
  36. }
  37. });
  38. }
  39. /**获得Json资源 */
  40. public static getResources (name: string): any {
  41. let _name: string = name;
  42. if (name.includes("/") == true) {
  43. _name = name.substring(name.indexOf("/")+1);
  44. }
  45. return this.resources.get(_name) || (cc.resources.get(name, cc.JsonAsset) as cc.JsonAsset).json;
  46. }
  47. }