PrefabResources.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import EventManager from "../Event/EventManager";
  2. import { EventName } from "../Event/EventName";
  3. import Tool from "../Tool/Tool";
  4. /**
  5. * 预制体资源 - 数据层
  6. */
  7. export default class PrefabResources {
  8. /**资源列表 */
  9. private static resources: Map<string, any> = new Map<string, any>();
  10. /**资源包 */
  11. private static bundle: cc.AssetManager.Bundle = null;
  12. /**加载预制体资源 */
  13. public static loadPrefabResources (prefabRes: Array<string>, denominator: number, currentProgress: number, callback: Function) {
  14. Tool.log("加载预制体资源");
  15. let complete = () => {
  16. Tool.log(this.resources);
  17. Tool.log("加载完毕");
  18. Tool.log("");
  19. if (callback == null) {
  20. // 发送初始加载完成 本地消息
  21. EventManager.sendEvent(EventName.Res.Res_LoadComplete);
  22. }
  23. else {
  24. callback();
  25. }
  26. }
  27. if (prefabRes.length == 0) {
  28. complete();
  29. }
  30. else {
  31. cc.assetManager.loadBundle("res/prefab", (err: Error, prefab: cc.AssetManager.Bundle) => {
  32. this.bundle = prefab;
  33. this.bundle.load(prefabRes, cc.Asset, (finish: number, total: number, item: cc.AssetManager.RequestItem) => {
  34. // 发送初始加载进度 本地消息
  35. EventManager.sendEvent(EventName.Res.Res_LoadProgress, (finish/total)*denominator + currentProgress);
  36. }, (err, assets: Array<cc.Asset>) => {
  37. Tool.log(err);
  38. for (let index in assets) {
  39. this.resources.set(assets[index].name, assets[index]);
  40. }
  41. complete();
  42. });
  43. });
  44. }
  45. }
  46. /**获得预制体资源 */
  47. public static getResources (name: string): any {
  48. let _name: string = name;
  49. if (name.includes("/") == true) {
  50. _name = name.substring(name.indexOf("/")+1);
  51. }
  52. if (this.resources.get(_name) != null) {
  53. return this.resources.get(_name);
  54. }
  55. else {
  56. return this.bundle.get(_name);
  57. }
  58. }
  59. }