ResMgr.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { assetManager, AudioClip, director, ImageAsset, instantiate, JsonAsset, Node, Prefab, SkinnedMeshRenderer, Sprite, SpriteAtlas, SpriteFrame, Texture2D, TextureCube } from "cc"
  2. import { AssetType } from "./Enums";
  3. import { Global } from "./Global";
  4. import { PoolMgr } from "./PoolMagr";
  5. export default class ResMgr {
  6. private _abBundleMap: Object = {};
  7. private _atlasMap = {};
  8. public _jsonAssetMap = {};
  9. public _clipMap = {};
  10. public _loadStemp = null;
  11. private loadTime = 0;
  12. _totalTime = 0
  13. private static _ins: ResMgr = null;
  14. public static get ins() {
  15. if (!this._ins) {
  16. this._ins = new ResMgr();
  17. }
  18. return this._ins;
  19. }
  20. printTimer(name: string = "", end = false) {
  21. this.loadTime = Date.now() - this._loadStemp;
  22. this._loadStemp = Date.now();
  23. this._totalTime += this.loadTime
  24. console.log(name + ",load time===", this.loadTime, "ms")
  25. if (end) {
  26. console.log("Load finish, total time===", this._totalTime, "ms")
  27. }
  28. }
  29. public async loadBundle(index: number, ratio: number = 0): Promise<void> {
  30. if (!this._loadStemp) this._loadStemp = Date.now();
  31. const rate = Global.LoadingRate;
  32. const name = "Bundle" + index
  33. return new Promise<void>((resolve, reject) => {
  34. assetManager.loadBundle(name, (err: any, bundle: any) => {
  35. if (err) {
  36. console.error("Bundle" + index + " load error, error==", err)
  37. } else {
  38. if (index != 2) this._abBundleMap[index] = bundle;
  39. this.printTimer("Bundle" + index + "__" + "load success")
  40. Global.LoadingRate = rate + ratio;
  41. resolve && resolve();
  42. }
  43. })
  44. })
  45. }
  46. public async loadRes(index: number, type: any, ratio: number = 0): Promise<void> {
  47. const rate = Global.LoadingRate;
  48. return new Promise<void>((resolve, reject) => {
  49. this._abBundleMap[index].loadDir(type.path, type.type, (finished: number, total: number) => {
  50. // this._loadTools.setValue(idx, finished / total);
  51. if (ratio > 0) Global.LoadingRate = rate + ratio * finished / total
  52. }, (err: any, assets: any[]) => {
  53. if (err) {
  54. console.error("Error===", err);
  55. resolve && resolve();
  56. }
  57. let asset: any
  58. if (type == AssetType.Prefab) {
  59. for (let i = 0; i < assets.length; i++) {
  60. asset = assets[i];
  61. PoolMgr.ins.setPrefab(asset.data.name, asset);
  62. // Global.Debug && console.log("prefab name==", asset.data.name)
  63. }
  64. }
  65. if (type == AssetType.Sound) {
  66. for (let i = 0; i < assets.length; i++) {
  67. asset = assets[i];
  68. // Global.Debug && console.log("clip name==", asset.name)
  69. if (!this._clipMap[asset.name]) this._clipMap[asset.name] = asset
  70. }
  71. }
  72. if (type == AssetType.Atlas) {
  73. for (let i = 0; i < assets.length; i++) {
  74. asset = assets[i];
  75. // Global.Debug && console.log("atlas name==", asset.name)
  76. if (!this._atlasMap[asset.name]) this._atlasMap[asset.name] = asset
  77. }
  78. }
  79. if (type == AssetType.Json) {
  80. for (let i = 0; i < assets.length; i++) {
  81. asset = assets[i];
  82. // Global.Debug && console.log("json name==", asset.name)
  83. if (!this._jsonAssetMap[asset.name]) this._jsonAssetMap[asset.name] = asset.json
  84. }
  85. }
  86. this.printTimer("Bundle" + index + "__" + type.path + "loaded success")
  87. resolve && resolve();
  88. })
  89. })
  90. }
  91. public async loadPrefab(info): Promise<void> {
  92. const rate = Global.LoadingRate;
  93. return new Promise<void>((resolve, reject) => {
  94. this._abBundleMap[info.bundle].load(info.path + info.name, function (err, Prefab) {
  95. if (err) {
  96. console.error("Error info===", err);
  97. resolve && resolve();
  98. }
  99. PoolMgr.ins.setPrefab(info.name, Prefab);
  100. // console.log("预制体名字===", info.name);
  101. resolve && resolve();
  102. })
  103. }
  104. )
  105. }
  106. public getClip(name: string) {
  107. return this._clipMap[name];
  108. }
  109. }