ViewResources.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import EventManager from "../Event/EventManager";
  2. import { _EJsonRes } from "../Data/CommonDataType";
  3. import { EventName } from "../Event/EventName";
  4. import Tool from "../Tool/Tool";
  5. /**
  6. * 视窗资源 - 数据层
  7. */
  8. export default class ViewResources {
  9. /**资源列表 */
  10. private static resources: Array<any> = new Array<any>();
  11. /**资源包 */
  12. private static bundle: cc.AssetManager.Bundle = null;
  13. /**加载视窗资源 */
  14. public static loadViewResources (fguiRes: Array<string>, cocosRes: Array<string>,denominator: number, currentProgress: number, callback: Function) {
  15. Tool.log("加载视窗资源");
  16. let fguiMax: number = fguiRes.length-1;
  17. let imageMax: number = cocosRes.length;
  18. let max: number = fguiMax+imageMax;
  19. let count: number = 0;
  20. let loadCocosRes = () => {
  21. cc.assetManager.loadBundle("res/cocos", (err: Error, cocos: cc.AssetManager.Bundle) => {
  22. this.bundle = cocos;
  23. this.bundle.load(cocosRes, cc.SpriteFrame,
  24. (finish: number, total: number, item: cc.AssetManager.RequestItem) => {
  25. // 发送初始加载进度 本地消息
  26. EventManager.sendEvent(EventName.Res.Res_LoadProgress, (count/max)*denominator + currentProgress);
  27. count++;
  28. },
  29. (err: Error, assets: cc.Asset[]) => {
  30. Tool.log(err);
  31. let cocos: Array<cc.Asset> = this.resources["cocos"] || [];
  32. for (let i in assets) {
  33. cocos[assets[i].name] = assets[i];
  34. }
  35. this.resources["cocos"] = cocos;
  36. Tool.log(this.resources);
  37. Tool.log("加载完毕");
  38. Tool.log("");
  39. if (callback == null) {
  40. // 发送初始加载完成 本地消息
  41. EventManager.sendEvent(EventName.Res.Res_LoadComplete);
  42. }
  43. else {
  44. callback();
  45. }
  46. }
  47. );
  48. });
  49. }
  50. if (fguiRes.length > 0) {
  51. cc.assetManager.loadBundle("res/fgui", (err: Error, bundle: cc.AssetManager.Bundle) => {
  52. for (let j: number = 0; j < fguiRes.length; j++) {
  53. fgui.UIPackage.loadPackage(bundle, fguiRes[j],
  54. (err, data: fgui.UIPackage) => {
  55. Tool.log(err);
  56. this.resources.push(data);
  57. if (count < fguiMax) {
  58. // 发送初始加载进度 本地消息
  59. EventManager.sendEvent(EventName.Res.Res_LoadProgress, (count/max)*denominator + currentProgress);
  60. }
  61. else {
  62. loadCocosRes();
  63. }
  64. count++;
  65. }
  66. );
  67. }
  68. });
  69. }
  70. else {
  71. loadCocosRes();
  72. }
  73. }
  74. /**获得视窗资源 */
  75. public static getResources (name: string): any {
  76. let _name: string = name;
  77. if (name.includes("/") == true) {
  78. _name = name.substring(name.indexOf("/")+1);
  79. }
  80. return this.resources["cocos"][_name] || this.bundle.get(name);
  81. }
  82. }