BenzAssetManager.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { Dictionary } from "../utils/Dictionary";
  2. import { IBenzAsset } from "./IBenzAsset";
  3. import { Log, LOG_TAG } from "../../framework/log/Log";
  4. export class BenzAssetManager {
  5. private static readonly MAX_RETRY_COUNT: number = 3;
  6. private static _instance: BenzAssetManager = new BenzAssetManager();
  7. private _cacheAssetDic = new Dictionary<cc.Asset>();
  8. private _currentIndex: number = 0;
  9. private _lastIndex: number = 0;
  10. private _currentAssets: IBenzAsset[];
  11. private _retryCount: number = 0;
  12. public static getInstance(): BenzAssetManager {
  13. return this._instance;
  14. }
  15. public loadResource(asset: IBenzAsset, completeCallback: (resource: any) => void, errorCallback: (error: Error) => void = null): void {
  16. if(this.hasAsset(asset.fileName)){
  17. Log.log(LOG_TAG.DEBUG,"[BenzAssetManager] Load Hit Cache: {0}"+ asset.fileName);
  18. completeCallback(this.getAsset(asset.fileName));
  19. return;
  20. }
  21. cc.loader.loadRes(asset.fileName, asset.type, function (error:Error, resource: any) {
  22. try {
  23. if(error){
  24. Log.log(LOG_TAG.DEBUG,"[BenzAssetManager] Load Error: {0}"+ asset.fileName);
  25. if (errorCallback){
  26. errorCallback(error);
  27. }
  28. }else{
  29. completeCallback(resource);
  30. cc.loader.setAutoReleaseRecursively(resource, asset.isAutoRelease);
  31. }
  32. } catch (e) {
  33. Log.error(e);
  34. if (errorCallback){
  35. errorCallback(e);
  36. }
  37. }
  38. });
  39. }
  40. public loadSceneResource(assetArray: IBenzAsset[], onProgress?: (completedCount: number, totalCount: number, item: any) => void, onLoaded?: (error: Error) => void): void {
  41. if(!assetArray || assetArray.length <= 0){
  42. if(onLoaded){
  43. onLoaded(null);
  44. }
  45. return;
  46. }
  47. let self = this;
  48. self._currentIndex = 0;
  49. self._lastIndex = assetArray.length - 1;
  50. self._currentAssets = assetArray;
  51. self.loadSceneResourceHandle(assetArray[self._currentIndex], onProgress, onLoaded);
  52. }
  53. protected loadSceneResourceHandle(asset: IBenzAsset, onProgress?: (completedCount: number, totalCount: number, item: any) => void, onLoaded?: (error: Error) => void): void {
  54. Log.log(LOG_TAG.DEBUG,"[BenzAssetManager] Load {0}"+ asset.fileName);
  55. let self = this;
  56. this.loadResource(asset, function(resource: any){
  57. if(!asset.isAutoRelease){
  58. Log.log(LOG_TAG.DEBUG,"[BenzAssetManager] Add {0}"+ asset.fileName);
  59. self.addAsset(asset.fileName, resource);
  60. }
  61. if(self._currentIndex >= self._lastIndex){
  62. if(onLoaded){
  63. onLoaded(null);
  64. }
  65. return;
  66. }
  67. if(onProgress){
  68. onProgress(self._currentIndex + 1, self._lastIndex + 1, resource);
  69. }
  70. self._retryCount = 0;
  71. self._currentIndex++;
  72. self.loadSceneResourceHandle(self._currentAssets[self._currentIndex], onProgress, onLoaded);
  73. }, function(error: Error){
  74. if(self._retryCount >= BenzAssetManager.MAX_RETRY_COUNT){
  75. if(onLoaded){
  76. onLoaded(error);
  77. }
  78. return;
  79. }
  80. self._retryCount++;
  81. self.loadSceneResourceHandle(self._currentAssets[self._currentIndex], onProgress, onLoaded);
  82. });
  83. }
  84. public getAsset(fileName: string): cc.Asset {
  85. return this._cacheAssetDic.get(fileName);
  86. }
  87. public hasAsset(fileName: string): boolean {
  88. return this._cacheAssetDic.containsKey(fileName);
  89. }
  90. public addAsset(fileName: string, asset: cc.Asset): void {
  91. if (!asset || !asset.isValid){
  92. Log.log(LOG_TAG.DEBUG,"[BenzAssetManager] Asset {0} is null"+ fileName);
  93. return;
  94. }
  95. cc.loader.setAutoReleaseRecursively(asset, false);
  96. this._cacheAssetDic.set(fileName, asset);
  97. }
  98. public removeAsset(fileName: string): void {
  99. if (!this._cacheAssetDic.containsKey(fileName)){
  100. return;
  101. }
  102. cc.loader.setAutoReleaseRecursively(this._cacheAssetDic.get(fileName), true);
  103. this._cacheAssetDic.remove(fileName);
  104. }
  105. public clearCache(): void {
  106. // this._cacheAssetDic.forEach(function(k:string, v:cc.SpriteAtlas) {
  107. // if(v){
  108. // var deps = cc.loader.getDependsRecursively(v);
  109. // cc.loader.release(deps);
  110. // v.destroy();
  111. // }
  112. // });
  113. // this._cacheAssetDic.clear();
  114. }
  115. }