16b2f4ee-7186-491c-a346-8659540364cf.js 8.3 KB

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