LoadMgr.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import Global from "../Global";
  2. import GameLog from "./GameLogMgr";
  3. import Texture2D = cc.Texture2D;
  4. import AudioClip = cc.AudioClip;
  5. import GameLogMgr from "./GameLogMgr";
  6. import TestMgr from "../Test";
  7. import Bundle = cc.AssetManager.Bundle;
  8. import url = cc.url;
  9. export default class LoadMgr {
  10. private static _sprite = {};
  11. private static alreadyLoadBundle: Map<string, Bundle> = new Map<string, Bundle>()
  12. public static loadBundle(bundleNames: string[]) {
  13. return new Promise((resolve, reject) => {
  14. let functions: any[] = []
  15. for (let i = 0; i < bundleNames.length; i++) {
  16. let name = bundleNames[i]
  17. if (!this.alreadyLoadBundle.has(name)) {
  18. functions.push(this.loadBundle_Single(name))
  19. }
  20. }
  21. Promise.all(functions).then((data) => {
  22. resolve(data)
  23. }, () => {
  24. reject(false)
  25. })
  26. })
  27. }
  28. public static loadBundle_Single(name: string) {
  29. return new Promise((resolve1, reject1) => {
  30. cc.assetManager.loadBundle(name, (err, data) => {
  31. if (err) {
  32. GameLogMgr.error("加载分包失败!!!!!!!!!", err, "name :", name)
  33. reject1(false)
  34. return
  35. }
  36. this.alreadyLoadBundle.set(name, data)
  37. resolve1(data)
  38. })
  39. })
  40. }
  41. public static judgeBundleLoad(name: string): boolean {
  42. return this.alreadyLoadBundle.has(name)
  43. }
  44. public static getBundle(bundle_name: string): Bundle {
  45. return this.alreadyLoadBundle.get(bundle_name)
  46. }
  47. //提前初始化所有分包
  48. public static init_bundleMgr() {
  49. this.loadBundle_Single("homeView").then()
  50. this.loadBundle_Single("gameView").then()
  51. this.loadBundle_Single("homeView").then()
  52. this.loadBundle_Single("treaView").then()
  53. this.loadBundle_Single("oneBox").then()
  54. }
  55. /**
  56. * 加载图片
  57. * @param sprite
  58. * @param _url
  59. * @param bundle 图片所在的分包
  60. * @param needActive
  61. */
  62. public static loadSprite(sprite: cc.Sprite, _url: string, bundle: Bundle = this.getBundle("sub"), needActive = true) {
  63. let id = bundle.name + "/" + _url
  64. return new Promise((resolve, reject) => {
  65. if (this._sprite.hasOwnProperty(id)) {
  66. sprite.spriteFrame = this._sprite[id];
  67. if (needActive) {
  68. sprite.node.active = true;
  69. }
  70. resolve(this._sprite[id]);
  71. return;
  72. }
  73. bundle.load("image/" + _url, cc.SpriteFrame, (err: Error, spf: cc.SpriteFrame) => {
  74. if (err) {
  75. GameLog.error(id, ' 图片加载错误 ', err);
  76. reject(false);
  77. return;
  78. }
  79. this._sprite[id] = spf;
  80. sprite.spriteFrame = spf;
  81. if (needActive) {
  82. sprite.node.active = true;
  83. }
  84. resolve(spf);
  85. });
  86. });
  87. }
  88. private static _remote_Sprite: Map<string, cc.SpriteFrame> = new Map<string, cc.SpriteFrame>();
  89. public static loadRemoteSprite(_url: string, sprite: cc.Sprite = null, needActive: boolean = true) {
  90. return new Promise((resolve, reject) => {
  91. if (this._remote_Sprite.get(_url)) {
  92. if (sprite) {
  93. sprite.spriteFrame = this._remote_Sprite.get(_url);
  94. }
  95. if (needActive) {
  96. if (sprite) {
  97. sprite.node.active = true;
  98. }
  99. }
  100. resolve(this._remote_Sprite.get(_url))
  101. } else {
  102. cc.assetManager.loadRemote(_url, Texture2D, (err, texture: Texture2D) => {
  103. if (texture.width == 0) {
  104. let path = cc.assetManager.cacheManager.getTemp(_url);
  105. cc.assetManager.loadRemote(path, (err, sp: cc.Texture2D) => {
  106. if (err) {
  107. GameLog.warn("第二次加载远程图片失败", err);
  108. reject(false);
  109. return;
  110. }
  111. this._remote_Sprite.set(_url, new cc.SpriteFrame(sp));
  112. });
  113. if (sprite) {
  114. sprite.spriteFrame = this._remote_Sprite.get(_url);
  115. }
  116. resolve(this._remote_Sprite.get(_url));
  117. } else {
  118. if (err) {
  119. GameLog.warn("加载远程图片失败", err);
  120. reject(false);
  121. return;
  122. }
  123. this._remote_Sprite.set(_url, new cc.SpriteFrame((texture)));
  124. }
  125. if (needActive) {
  126. if (sprite) {
  127. sprite.node.active = true;
  128. sprite.spriteFrame = this._remote_Sprite.get(_url);
  129. // sprite.spriteFrame = new cc.SpriteFrame(texture)
  130. }
  131. }
  132. resolve(this._remote_Sprite.get(_url));
  133. });
  134. }
  135. });
  136. }
  137. private static _prefabCaches = {};
  138. /**
  139. * 加载 预制体
  140. * @param _url
  141. * @param bundle 预制体所在的分包
  142. */
  143. public static loadPrefab(_url: string, bundle: Bundle = this.getBundle("sub")) {
  144. let id = bundle.name + "/" + _url
  145. return new Promise((resolve, reject) => {
  146. if (this._prefabCaches.hasOwnProperty(id)) {
  147. resolve(this._prefabCaches[id]);
  148. return;
  149. }
  150. bundle.load("prefab/" + _url, cc.Prefab, (err: Error, prefab: cc.Prefab) => {
  151. if (err) {
  152. GameLog.error('setPrefab error', err, id);
  153. reject(false);
  154. return;
  155. }
  156. this._prefabCaches[id] = prefab;
  157. resolve(prefab);
  158. });
  159. });
  160. }
  161. private static _audio_caches: Map<string, cc.AudioClip> = new Map<string, cc.AudioClip>();
  162. public static load_AudioClip(_url: string, bundle: Bundle = this.getBundle("sub")) {
  163. let id = bundle.name + "/" + _url
  164. return new Promise((resolve, reject) => {
  165. if (this._audio_caches.get(id)) {
  166. let audioClip = this._audio_caches.get(id);
  167. resolve(audioClip);
  168. } else {
  169. bundle.load("audio/" + _url, cc.AudioClip, (err, audio: AudioClip) => {
  170. if (err) {
  171. GameLog.error("加载音频失败", err, "url : ", id);
  172. reject(null);
  173. return;
  174. }
  175. this._audio_caches.set(id, audio);
  176. resolve(audio);
  177. });
  178. }
  179. });
  180. }
  181. private static _AtlasCaches = {};
  182. /**
  183. * 加载 图集文件
  184. * @private
  185. */
  186. public static loadAtlas(_url: string, bundle: Bundle = this.getBundle("sub")) {
  187. let id = bundle.name + "/" + _url
  188. return new Promise((resolve, reject) => {
  189. if (this._AtlasCaches.hasOwnProperty(id)) {
  190. resolve(this._AtlasCaches[id]);
  191. return;
  192. }
  193. bundle.load("image/" + _url, cc.SpriteAtlas, (err: Error, atlas: cc.SpriteAtlas) => {
  194. if (err) {
  195. GameLog.log('setAtlas error', err, id);
  196. reject(false);
  197. return;
  198. }
  199. this._AtlasCaches[id] = atlas;
  200. resolve(atlas);
  201. });
  202. });
  203. }
  204. }