AssetsBundleMgr.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // 预加载某个bundle
  2. // 释放某个bundle,并清理相关的UI
  3. // 打开UI前,根据其归属bundle加载后再打开
  4. // 同一个bundle下的图片,预制体,放在一个文件夹下(脚本不可放在bundle下)
  5. import Load from "./Load"
  6. export default class AssetsBundleMgr {
  7. private static bundleList: { [key: string]: cc.AssetManager.Bundle } = {}
  8. // //bundle预加载
  9. // public static preloadBundle(bundleName: string) {
  10. // if (this.bundleList[bundleName] != null) {
  11. // return
  12. // }
  13. // cc.assetManager.loadBundle(bundleName, (err, bundle: cc.AssetManager.Bundle) => {
  14. // if (err) {
  15. // console.warn("bundle预加载失败: " + bundleName)
  16. // return
  17. // }
  18. // this.bundleList[bundleName] = bundle
  19. // })
  20. // }
  21. /** 获取缓存中的资源 */
  22. public static get<T extends cc.Asset>(bundleKey: string, path: string, type?: any): T | null {
  23. if (!cc.assetManager.getBundle(bundleKey)) return null;
  24. return cc.assetManager.getBundle(bundleKey).get(path, type);
  25. }
  26. //加载bundle
  27. public static loadBundle(bundleName: string, callback = (error, bundle: cc.AssetManager.Bundle) => { }) {
  28. if (this.bundleList[bundleName] != null) {
  29. callback(null, this.bundleList[bundleName])
  30. } else {
  31. cc.assetManager.loadBundle(bundleName, (err, bundle: cc.AssetManager.Bundle) => {
  32. if (err) {
  33. callback(err, null)
  34. return
  35. }
  36. this.bundleList[bundleName] = bundle
  37. callback(null, bundle)
  38. })
  39. }
  40. }
  41. // 加载bundle中的prefab
  42. public static loadBundlePrefab(url: string, callback = (error, prefab: cc.Prefab) => { }) {
  43. let bundleName = url.split("/")[0]
  44. let realUrl = url.slice(url.indexOf("/") + 1)
  45. if (this.bundleList[bundleName] != null) {
  46. this.bundleList[bundleName].load(realUrl, cc.Prefab, (err, prefab: cc.Prefab) => {
  47. if (err) {
  48. callback(err, null)
  49. return
  50. }
  51. callback(null, prefab)
  52. })
  53. } else {
  54. cc.assetManager.loadBundle(bundleName, (err, bundle: cc.AssetManager.Bundle) => {
  55. if (err) {
  56. callback(err, null)
  57. return
  58. }
  59. this.bundleList[bundleName] = bundle
  60. this.bundleList[bundleName].load(realUrl, cc.Prefab, (err, prefab: cc.Prefab) => {
  61. if (err) {
  62. callback(err, null)
  63. return
  64. }
  65. callback(null, prefab)
  66. })
  67. })
  68. }
  69. }
  70. /** 同步加载bundle中的prefab */
  71. public static async loadAsynBundlePrefab(url: string): Promise<cc.Prefab> {
  72. return new Promise((resolve, reject) => {
  73. let bundleName = url.split("/")[0]
  74. let realUrl = url.slice(url.indexOf("/") + 1)
  75. if (this.bundleList[bundleName] != null) {
  76. this.bundleList[bundleName].load(realUrl, cc.Prefab, (err, prefab: cc.Prefab) => {
  77. if (err) {
  78. reject(null);
  79. return
  80. }
  81. resolve(prefab)
  82. })
  83. } else {
  84. cc.assetManager.loadBundle(bundleName, (err, bundle: cc.AssetManager.Bundle) => {
  85. if (err) {
  86. reject(null);
  87. return
  88. }
  89. this.bundleList[bundleName] = bundle
  90. this.bundleList[bundleName].load(realUrl, cc.Prefab, (err, prefab: cc.Prefab) => {
  91. if (err) {
  92. reject(null);
  93. return
  94. }
  95. resolve(prefab)
  96. })
  97. })
  98. }
  99. });
  100. }
  101. // 加载bundle中的资源
  102. public static loadBundleAsset<T extends cc.Asset>(url: string, T, callback: Function) {
  103. let bundleName = url.split("/")[0]
  104. let realUrl = url.slice(url.indexOf("/") + 1)
  105. cc.assetManager.loadBundle(bundleName, (err, bundle: cc.AssetManager.Bundle) => {
  106. if (err) {
  107. callback(err, null)
  108. return
  109. }
  110. this.bundleList[bundleName] = bundle
  111. this.bundleList[bundleName].load(realUrl, T, (err, asset: T) => {
  112. if (err) {
  113. callback(err, null)
  114. return
  115. }
  116. callback(null, asset)
  117. })
  118. })
  119. }
  120. // 加载bundle中的资源文件夹
  121. public static loadBundleAssetDir(url: string, callback = (error, asset: cc.Asset[]) => { }) {
  122. let bundleName = url.split("/")[0]
  123. let realUrl = url.slice(url.indexOf("/") + 1)
  124. cc.assetManager.loadBundle(bundleName, (err, bundle: cc.AssetManager.Bundle) => {
  125. if (err) {
  126. callback(err, null)
  127. return
  128. }
  129. this.bundleList[bundleName] = bundle
  130. this.bundleList[bundleName].loadDir(realUrl, (err, assets: cc.Asset[]) => {
  131. if (err) {
  132. callback(err, null)
  133. return
  134. }
  135. callback(null, assets)
  136. })
  137. })
  138. }
  139. //释放单个bundle
  140. public static releaseBundle(bundleName: string) {
  141. if (this.bundleList[bundleName] != null) {
  142. console.log("releaseBundle:" + bundleName)
  143. this.bundleList[bundleName].releaseAll()
  144. Load.clearByBundle(bundleName)
  145. delete this.bundleList[bundleName]
  146. }
  147. }
  148. //释放所有bundle
  149. public static releaseAllBundle() {
  150. for (const bundleName in this.bundleList) {
  151. console.log("releaseBundle:" + bundleName)
  152. const element = this.bundleList[bundleName]
  153. element.releaseAll()
  154. Load.clearByBundle(bundleName)
  155. }
  156. this.bundleList = {}
  157. }
  158. }