ResMgr.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { cocosz } from "./CocosZ";
  2. export default class ResMgr {
  3. private static _inst: ResMgr;
  4. public static get inst(): ResMgr {
  5. if (!ResMgr._inst) {
  6. ResMgr._inst = new ResMgr();
  7. ResMgr._inst._init();
  8. }
  9. return ResMgr._inst;
  10. }
  11. private _prefabDic: any = {};
  12. private _imgDic: any = {};
  13. private _atlasDic: any = {};
  14. private _audioDic: any = {};
  15. private _jsonDic: any = {};
  16. private _init() {
  17. // 根据平台做对应设置
  18. }
  19. /**
  20. * 缓存cocosz的音效
  21. * @param res
  22. */
  23. public cacheCocoszAudio() {
  24. for (let i = 0; i < cocosz.audioList.length; i++) {
  25. const res = cocosz.audioList[i];
  26. this._cacheRes(res, cc.AudioClip);
  27. }
  28. cocosz.audioList = null;
  29. }
  30. /**
  31. * 加载资源
  32. * @param path 资源路径
  33. * @param type 资源类型
  34. * @param complete 加载完成回调
  35. */
  36. public loadRes(path: string, type: typeof cc.Asset, progress: Function, complete: Function) {
  37. let bundle = cocosz.getBundleWithPath(path);
  38. if (bundle) {
  39. bundle.load(path, type, (completedCount: number, totalCount: number, item: any) => {
  40. progress && progress(completedCount, totalCount, item);
  41. }, (error: Error, resource: any) => {
  42. if (error) {
  43. cc.log("加载资源", path, "失败");
  44. }
  45. complete && complete(error, resource);
  46. });
  47. }
  48. }
  49. /**
  50. * 加载资源数组并且添加到本地缓存
  51. * @param url 资源路径数组
  52. * @param type 资源类型
  53. * @param progress 加载进度回调
  54. * @param complete 加载完成回调
  55. */
  56. public loadAndCacheResArray(urls: any[], type: typeof cc.Asset, progress: Function, complete: Function) {
  57. for (let i = 0; i < urls.length; i++) {
  58. this.loadAndCacheRes(urls[i], type, progress, complete);
  59. }
  60. }
  61. /**
  62. * 加载资源并且添加到本地缓存
  63. * @param url 资源路径
  64. * @param type 资源类型
  65. * @param progress 加载进度回调
  66. * @param complete 加载完成回调
  67. */
  68. public loadAndCacheRes(url: any, type: typeof cc.Asset, progress: Function, complete: Function) {
  69. let path = url.path ? url.path : url;
  70. let bundle = cocosz.getBundleWithPath(path);
  71. if (bundle) {
  72. bundle.load(path, type, (completedCount: number, totalCount: number, item: any) => {
  73. progress && progress(completedCount, totalCount, item);
  74. }, (error: Error, resource: any) => {
  75. if (error) {
  76. cc.log("加载缓存资源", path, "失败");
  77. } else {
  78. this._cacheRes(resource, type);
  79. }
  80. complete && complete(error, resource);
  81. });
  82. }
  83. }
  84. private _cacheRes(res: any, type: typeof cc.Asset) {
  85. if (type == cc.Prefab) {
  86. this._cachPrefab(res);
  87. } else if (type == cc.SpriteFrame) {
  88. this._cachTexture(res);
  89. } else if (type == cc.SpriteAtlas) {
  90. this._cachSpriteAtlas(res);
  91. } else if (type == cc.AudioClip) {
  92. this._cachAudioClip(res);
  93. } else if (type == cc.JsonAsset) {
  94. this._cachJsonAsset(res);
  95. }
  96. }
  97. private _cachPrefab(res: cc.Prefab) {
  98. if (res) {
  99. res.addRef();
  100. this._prefabDic[res.name] = res;
  101. }
  102. }
  103. private _cachTexture(res: cc.SpriteFrame) {
  104. if (res) {
  105. res.addRef();
  106. this._imgDic[res.name] = res;
  107. }
  108. }
  109. private _cachSpriteAtlas(res: cc.SpriteAtlas) {
  110. if (res) {
  111. res.addRef();
  112. this._atlasDic[res.name] = res;
  113. let spframes: cc.SpriteFrame[] = res.getSpriteFrames();
  114. for (let i = 0; i < spframes.length; i++) {
  115. this._cachTexture(spframes[i]);
  116. }
  117. }
  118. }
  119. private _cachAudioClip(res: cc.AudioClip) {
  120. if (res) {
  121. res.addRef();
  122. this._audioDic[res.name] = res;
  123. }
  124. }
  125. private _cachJsonAsset(res: cc.JsonAsset) {
  126. if (res) {
  127. res.addRef();
  128. this._jsonDic[res.name] = res;
  129. }
  130. }
  131. /**
  132. * 获取本地缓存资源
  133. * @param name 资源名称
  134. * @param type 类型
  135. */
  136. public getRes(name: string, type: typeof cc.Asset) {
  137. switch (type) {
  138. case cc.Prefab: {
  139. return this._check(name, this._prefabDic[name]);
  140. }
  141. case cc.SpriteFrame: {
  142. return this._check(name, this._imgDic[name]);
  143. }
  144. case cc.AudioClip: {
  145. return this._check(name, this._audioDic[name]);
  146. }
  147. case cc.JsonAsset: {
  148. return this._check(name, this._jsonDic[name]);
  149. }
  150. default: {
  151. cc.log("资源类型不存在:" + type);
  152. return null;
  153. }
  154. }
  155. }
  156. private _check(name: string, res: any) {
  157. if (res && res.isValid) {
  158. return res;
  159. } else {
  160. cc.log("资源不存在:" + name);
  161. return null;
  162. }
  163. }
  164. /**
  165. * 释放一组资源
  166. * @param urlList 资源路径数组
  167. * @param type 资源类型
  168. */
  169. public releaseResArray(urlList: any[], type: typeof cc.Asset) {
  170. urlList.forEach(url => {
  171. let name = url.path.split("/").pop();
  172. if (name) {
  173. this.releaseSingleRes(name, type);
  174. }
  175. });
  176. }
  177. /**
  178. * 释放单个资源
  179. * @param res 单个资源
  180. */
  181. public releaseSingleRes(name: string, type: typeof cc.Asset) {
  182. switch (type) {
  183. case cc.Prefab: {
  184. if (this._prefabDic[name] && cc.isValid(this._prefabDic[name])) {
  185. this._prefabDic[name].decRef();
  186. cc.assetManager.releaseAsset(this._prefabDic[name]);
  187. this._prefabDic[name] = null;
  188. // cc.log(`释放Prefab资源 ${name} 成功!`);
  189. }
  190. break;
  191. }
  192. case cc.SpriteFrame: {
  193. if (this._imgDic[name] && cc.isValid(this._imgDic[name])) {
  194. this._imgDic[name].decRef();
  195. cc.assetManager.releaseAsset(this._imgDic[name]);
  196. this._imgDic[name] = null;
  197. // cc.log(`释放SpriteFrame资源 ${name} 成功!`);
  198. }
  199. break;
  200. }
  201. case cc.SpriteAtlas: {
  202. if (this._atlasDic[name] && cc.isValid(this._atlasDic[name])) {
  203. this._atlasDic[name].decRef();
  204. cc.assetManager.releaseAsset(this._atlasDic[name]);
  205. this._atlasDic[name] = null;
  206. // cc.log(`释放SpriteFrame资源 ${name} 成功!`);
  207. }
  208. break;
  209. }
  210. case cc.AudioClip: {
  211. if (this._audioDic[name] && cc.isValid(this._audioDic[name])) {
  212. this._audioDic[name].decRef();
  213. cc.assetManager.releaseAsset(this._audioDic[name]);
  214. this._audioDic[name] = null;
  215. // cc.log(`释放AudioClip资源 ${name} 成功!`);
  216. }
  217. break;
  218. }
  219. case cc.JsonAsset: {
  220. if (this._jsonDic[name] && cc.isValid(this._jsonDic[name])) {
  221. this._jsonDic[name].decRef();
  222. cc.assetManager.releaseAsset(this._jsonDic[name]);
  223. this._jsonDic[name] = null;
  224. // cc.log(`释放JsonAsset资源 ${name} 成功!`);
  225. }
  226. break;
  227. }
  228. default: {
  229. cc.log("释放资源的类型出错");
  230. }
  231. }
  232. }
  233. }