Loader.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import common from "../common/common";
  2. class Loader {
  3. private static _instance: Loader;
  4. public static getInstance(): Loader {
  5. if (this._instance) {
  6. return this._instance;
  7. }
  8. this._instance = new Loader();
  9. return this._instance;
  10. };
  11. /**
  12. * 加载bundle资源
  13. * @param bundleName
  14. */
  15. loadBundle(bundleName: string): Promise<cc.AssetManager.Bundle> {
  16. return new Promise((resolve) => {
  17. cc.assetManager.loadBundle(bundleName, null, (err, asset: cc.AssetManager.Bundle) => {
  18. if (err) {
  19. common.log("加载bundle失败:", bundleName);
  20. resolve(null);
  21. }
  22. else {
  23. common.log("加载bundle:", bundleName);
  24. resolve(asset);
  25. }
  26. });
  27. });
  28. }
  29. loadResLoacl(path): Promise<any>{
  30. return new Promise((resolve) => {
  31. cc.resources.load(path, function (err, asset: any) {
  32. if (err) {
  33. common.log("加载失败:", path);
  34. resolve(null);
  35. }
  36. else {
  37. common.log("加载资源:", path);
  38. resolve(asset);
  39. }
  40. });
  41. });
  42. }
  43. /**
  44. * 加载资源:bundle内
  45. * @param type
  46. * @param bundle
  47. * @param path
  48. */
  49. loadBundleRes(type, bundle, path) {
  50. switch (type) {
  51. case "img":
  52. return this.loadBundleImg(bundle, path);
  53. case "audio":
  54. return this.loadBundleAudio(bundle, path);
  55. case "json":
  56. return this.loadBundleJson(bundle, path);
  57. case "font":
  58. return this.loadBundleFont(bundle, path);
  59. case "spine":
  60. return this.loadBundleSpine(bundle, path);
  61. case "prefab":
  62. return this.loadBundlePrefab(bundle, path);
  63. default:
  64. break;
  65. }
  66. }
  67. /**
  68. * 加载图片资源:bundle内
  69. * @param bundle
  70. * @param path
  71. * @param callBack
  72. */
  73. loadBundleImg(bundle: cc.AssetManager.Bundle, path: string): Promise<cc.Texture2D> {
  74. return new Promise((resolve) => {
  75. bundle.load(path, cc.Texture2D, (err, asset: cc.Texture2D) => {
  76. if (err) {
  77. common.log("加载图片失败:", path);
  78. }
  79. else {
  80. common.log("加载图片:", path);
  81. resolve(asset);
  82. }
  83. });
  84. })
  85. }
  86. /**
  87. * 加载音频资源:bundle内
  88. * @param bundle
  89. * @param path
  90. */
  91. loadBundleAudio(bundle: cc.AssetManager.Bundle, path: string): Promise<cc.AudioClip> {
  92. return new Promise((resolve) => {
  93. bundle.load(path, cc.AudioClip, (err, asset: cc.AudioClip) => {
  94. if (err) {
  95. common.log("加载音频失败:", path);
  96. }
  97. else {
  98. common.log("加载音频:", path);
  99. resolve(asset);
  100. }
  101. });
  102. });
  103. }
  104. /**
  105. * 加载json资源:bundle内
  106. * @param bundle
  107. * @param path
  108. */
  109. loadBundleJson(bundle: cc.AssetManager.Bundle, path): Promise<cc.JsonAsset> {
  110. return new Promise((resolve) => {
  111. bundle.load(path, cc.JsonAsset, (err, asset: cc.JsonAsset) => {
  112. if (err) {
  113. common.log("加载json失败:", path);
  114. }
  115. else {
  116. common.log("加载json:", path);
  117. resolve(asset);
  118. }
  119. });
  120. });
  121. }
  122. /**
  123. * 加载字体资源:bundle内
  124. * @param bundle
  125. * @param path
  126. */
  127. loadBundleFont(bundle: cc.AssetManager.Bundle, path: string): Promise<cc.Font> {
  128. return new Promise((resolve) => {
  129. bundle.load(path, cc.Font, (err, asset: cc.Font) => {
  130. if (err) {
  131. common.log("加载字体失败: ", path);
  132. }
  133. else {
  134. common.log("加载字体: ", path);
  135. resolve(asset);
  136. }
  137. });
  138. });
  139. }
  140. /**
  141. * 加载spine资源:bundle内
  142. * @param bundle
  143. * @param path
  144. */
  145. loadBundleSpine(bundle: cc.AssetManager.Bundle, path: string): Promise<sp.SkeletonData> {
  146. return new Promise((resolve) => {
  147. bundle.load(path, sp.SkeletonData, (err, asset: sp.SkeletonData) => {
  148. if (err) {
  149. common.log("加载spine失败: ", path);
  150. }
  151. else {
  152. common.log("加载spine: ", path);
  153. resolve(asset);
  154. }
  155. });
  156. });
  157. }
  158. /**
  159. * 加载prefab资源:bundle内
  160. * @param bundle
  161. * @param path
  162. */
  163. loadBundlePrefab(bundle: cc.AssetManager.Bundle, path): Promise<cc.Prefab> {
  164. return new Promise((resolve) => {
  165. bundle.load(path, cc.Prefab, (err, asset: cc.Prefab) => {
  166. if (err) {
  167. common.log("加载prefab失败: ", path);
  168. }
  169. else {
  170. common.log("加载prefab: ", path);
  171. resolve(asset);
  172. }
  173. });
  174. });
  175. }
  176. /**
  177. * 加载图片资源:bundle内
  178. * @param bundle
  179. * @param path
  180. * @param callBack
  181. */
  182. loadBundleDir(bundle: cc.AssetManager.Bundle, path: string): Promise<cc.Asset[]> {
  183. return new Promise((resolve) => {
  184. bundle.loadDir(path, cc.Asset, (err, assets: cc.Asset[]) => {
  185. if (err) {
  186. common.log("加载图片失败:", path);
  187. }
  188. else {
  189. common.log("加载图片:", path);
  190. resolve(assets);
  191. }
  192. });
  193. })
  194. }
  195. /**
  196. * 加载其他资源:bundle内
  197. * @param bundle
  198. * @param path
  199. * @param callBack
  200. */
  201. loadBundleOther(bundle: cc.AssetManager.Bundle, path: string): Promise<cc.Asset> {
  202. return new Promise((resolve) => {
  203. bundle.load(path, cc.Asset, (err, assets: cc.Asset) => {
  204. if (err) {
  205. common.log("加载资源失败:", path);
  206. }
  207. else {
  208. common.log("加载资源:", path);
  209. resolve(assets);
  210. }
  211. });
  212. })
  213. }
  214. /**
  215. * 加载资源:远程加载
  216. * @param type
  217. * @param url
  218. */
  219. loadRemote(type, url) {
  220. switch (type) {
  221. case "img":
  222. return this.loadRemoteImg(url);
  223. case "mp3":
  224. return this.loadRemoteAudio(url);
  225. default:
  226. break;
  227. }
  228. };
  229. /**
  230. * 加载图片资源:远程加载
  231. * @param url
  232. */
  233. loadRemoteImg(url) {
  234. return new Promise((resolve) => {
  235. cc.assetManager.loadRemote(url, (err: Error, asset: cc.Texture2D) => {
  236. if (err) {
  237. common.log("加载图片失败:", url);
  238. }
  239. else {
  240. common.log("加载图片:", url);
  241. resolve(asset);
  242. }
  243. });
  244. });
  245. };
  246. /**
  247. * 加载音频资源:远程加载
  248. * @param url
  249. */
  250. loadRemoteAudio(url) {
  251. return new Promise((resolve) => {
  252. cc.assetManager.loadRemote(url, (err: Error, asset: cc.AudioClip) => {
  253. if (err) {
  254. common.log("加载音频失败:", url);
  255. }
  256. else {
  257. common.log("加载音频:", url);
  258. resolve(asset);
  259. }
  260. });
  261. });
  262. };
  263. /**
  264. * 加载spine资源:远程加载
  265. * @param key
  266. * @param url
  267. * @param obj
  268. * @param callBack
  269. */
  270. loadRemoteSpine(key, url, obj, callBack) {
  271. let functionA = (texture, skeAtlas, skeJson) => {
  272. let skeletonData = new sp.SkeletonData();
  273. skeletonData.skeletonJson = skeJson.json;
  274. skeletonData.atlasText = skeAtlas.text;
  275. skeletonData.textures = [texture];
  276. if (skeletonData["textureNames"]) {
  277. skeletonData["textureNames"] = [key + ".png"];
  278. }
  279. obj[url] = skeletonData;
  280. common.log("加载spine:", url);
  281. callBack();
  282. };
  283. cc.assetManager.loadRemote(url + ".png", (err: Error, texture: cc.Texture2D) => {
  284. cc.assetManager.loadRemote(url + ".atlas", (error: Error, skeAtlas: cc.TextAsset) => {
  285. cc.assetManager.loadRemote(url + ".json", (error: Error, skeJson: cc.JsonAsset) => {
  286. functionA(texture, skeAtlas, skeJson);
  287. });
  288. });
  289. });
  290. };
  291. };
  292. export default Loader.getInstance();