resManager.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { Asset, assetManager, AssetManager } from "cc";
  2. export class resManager {
  3. private static _instance: resManager;
  4. private constructor() { }
  5. public static get instance(): resManager {
  6. if (!this._instance) {
  7. this._instance = new resManager();
  8. }
  9. return this._instance;
  10. }
  11. /**
  12. * 从bundle中加载某个资源,优先使用缓存中的
  13. * @param bundleName bundle名称
  14. * @param path 资源路径
  15. * @param assetType 资源类型
  16. * @param onProgress 加载进度回调
  17. * @param onComplete 加载成功回调
  18. */
  19. loadAsset(bundleName: string, path: string, assetType: any, onProgress?: Function, onComplete?: Function) {
  20. let bundle = assetManager.getBundle(bundleName);
  21. if (bundle && bundle.get(path, assetType)) {
  22. if (onComplete) {
  23. onComplete(null, bundle.get(path, assetType));
  24. }
  25. return;
  26. }
  27. let loadAssetFunc = () => {
  28. bundle.load(path, assetType, (finish: number, total: number) => {
  29. if (onProgress) {
  30. onProgress(finish, total);
  31. }
  32. }, (err, asset) => {
  33. if (err) {
  34. console.log("ResManager.loadAsset error:" + err.message, bundleName, path, err);
  35. if (onComplete) {
  36. onComplete(err);
  37. }
  38. return;
  39. }
  40. if (onComplete) {
  41. onComplete(null, asset);
  42. }
  43. });
  44. };
  45. if (!bundle) {
  46. assetManager.loadBundle(bundleName, (err, retBundle) => {
  47. if (err) {
  48. if (onComplete) {
  49. onComplete(err);
  50. }
  51. return;
  52. }
  53. bundle = retBundle;
  54. loadAssetFunc();
  55. });
  56. return;
  57. }
  58. loadAssetFunc();
  59. }
  60. /**
  61. * 加载某个bundle中的批量资源
  62. * @param bundleName bundle名称
  63. * @param pathArr 资源路径数组
  64. * @param assetType 资源类型
  65. * @param onProgress 进度回调
  66. * @param onComplete 结束回调
  67. */
  68. loadAssetByPathArr(bundleName: string, pathArr: string[], assetType: any, onProgress?: Function, onComplete?: Function) {
  69. let total = pathArr.length;
  70. if (total <= 0) {
  71. if (onComplete) {
  72. onComplete();
  73. }
  74. return;
  75. }
  76. let count = 0;
  77. let arr = [];
  78. for (let i = 0; i < total; i++) {
  79. let path = pathArr[i];
  80. this.loadAsset(bundleName, path, assetType, null, (err, asset) => {
  81. count++;
  82. arr.push(asset);
  83. if (onProgress) {
  84. onProgress(count, total, asset);
  85. }
  86. if (count >= total) {
  87. if (onComplete) {
  88. onComplete(arr);
  89. }
  90. }
  91. });
  92. }
  93. }
  94. /**
  95. * 预加载某个bundle中的批量资源
  96. * @param bundleName bundle名称
  97. * @param pathArr 资源路径数组
  98. * @param assetType 资源类型
  99. * @param onProgress 进度回调
  100. * @param onComplete 结束回调
  101. */
  102. preloadAssetByPathArr(bundleName: string, pathArr: string[], assetType: any, onProgress?: Function, onComplete?: Function) {
  103. let total = pathArr.length;
  104. if (total <= 0) {
  105. if (onComplete) {
  106. onComplete();
  107. }
  108. return;
  109. }
  110. let count = 0;
  111. let arr = [];
  112. for (let i = 0; i < total; i++) {
  113. let path = pathArr[i];
  114. this.loadAsset(bundleName, path, assetType, null, (err, asset) => {
  115. count++;
  116. arr.push(asset);
  117. if (onProgress) {
  118. onProgress(count, total, asset);
  119. }
  120. if (count >= total) {
  121. if (onComplete) {
  122. onComplete(arr);
  123. }
  124. }
  125. });
  126. }
  127. }
  128. /**
  129. * 加载bundle某个目录下的所有资源,目录中不要包含子目录,因为回调中拿不到资源的相对路径
  130. * @param bundleName bundle名称
  131. * @param dir 目录
  132. * @param assetType 资源类型
  133. * @param onProgress 进度回调
  134. * @param onComplete 成功回调
  135. */
  136. loadAssetByBundleDir(bundleName: string, dir: string, assetType: any, onProgress?: Function, onComplete?: Function) {
  137. let bundle = assetManager.getBundle(bundleName);
  138. let loadAssetFunc = () => {
  139. bundle.loadDir(dir, assetType, (finish: number, total: number, item: AssetManager.RequestItem) => {
  140. if (onProgress) {
  141. onProgress(finish, total, item);
  142. }
  143. }, (err, assetArr) => {
  144. if (err) {
  145. console.log("ResManager.loadAssetByBundleDir loadDir error:" + err.message, err);
  146. }
  147. if (onComplete) {
  148. onComplete(assetArr);
  149. }
  150. });
  151. };
  152. if (!bundle) {
  153. assetManager.loadBundle(bundleName, (err, retBundle) => {
  154. if (err) {
  155. console.log("ResManager.loadAssetByBundleDir loadBundle error:" + err.message, err);
  156. return;
  157. }
  158. bundle = retBundle;
  159. loadAssetFunc();
  160. });
  161. return;
  162. }
  163. loadAssetFunc();
  164. }
  165. /**
  166. * 直接从缓存中获取资源
  167. * @param bundleName bundle名称
  168. * @param path 资源路径
  169. */
  170. getAsset(bundleName: string, path: string): any {
  171. let bundle = assetManager.getBundle(bundleName);
  172. if (bundle) {
  173. return bundle.get(path);
  174. }
  175. return null;
  176. }
  177. releaseAsset(asset: Asset) {
  178. assetManager.releaseAsset(asset);
  179. }
  180. }