Load.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import Gamecfg from "../common/gameCfg"
  2. import { KindItem } from "../common/Xys"
  3. import { IconUrl } from "../data/const/ResConst"
  4. import { ItemKind } from "../data/const/TypeConst"
  5. import GameDataCenter from "../data/GameDataCenter"
  6. import AssetsBundleMgr from "./AssetsBundleMgr"
  7. /**
  8. * 动态加载资源
  9. */
  10. export default class Load {
  11. static loadPrefabByBundle(bundle: string, prefab: string, callback?: Function) {
  12. cc.assetManager.loadBundle(bundle, cc.Prefab, (err, bundle: cc.AssetManager.Bundle) => {
  13. if (err) {
  14. console.warn(err)
  15. return
  16. }
  17. bundle.load(prefab, cc.Prefab, function (err, prefab) {
  18. if (callback) {
  19. callback(prefab)
  20. }
  21. })
  22. })
  23. }
  24. static resList: { [bundleName: string]: { [key: string]: cc.SpriteFrame } } = {}
  25. // 根据详细地址加载图片资源
  26. static loadTexture(node: cc.Node, url: string, callback: Function = (success: boolean) => { }, needDelFirst: boolean = true) {
  27. if (node.getComponent(cc.Sprite) == null) {
  28. node.addComponent(cc.Sprite)
  29. }
  30. let bundleName = url.split("/")[0]
  31. if (this.resList[bundleName] != null && this.resList[bundleName][url] != null && this.resList[bundleName][url].isValid && this.resList[bundleName][url].loaded) {
  32. if (this.resList[bundleName][url].getTexture().isValid && this.resList[bundleName][url].getTexture().loaded) {
  33. node.getComponent(cc.Sprite).spriteFrame = this.resList[bundleName][url]
  34. callback(true)
  35. return
  36. }
  37. }
  38. if (needDelFirst) {
  39. node.getComponent(cc.Sprite).spriteFrame = null
  40. }
  41. if (node['imageTag'] == null) {
  42. node['imageTag'] = 1
  43. } else {
  44. if (node['imageTag'] > 100) {
  45. node['imageTag'] = 1
  46. } else {
  47. node['imageTag'] += 1
  48. }
  49. }
  50. let _tempImageTag = node['imageTag']
  51. if (!bundleName || bundleName == "") return;
  52. AssetsBundleMgr.loadBundle(bundleName, (error, bundle: cc.AssetManager.Bundle) => {
  53. if (error) {
  54. console.error(`bundle加载失败: ${bundleName}`);
  55. if (needDelFirst == false) {
  56. node.getComponent(cc.Sprite).spriteFrame = null
  57. }
  58. return;
  59. }
  60. let realUrl = url.slice(url.indexOf("/") + 1)
  61. bundle.load(realUrl, cc.SpriteFrame, (err, asset: cc.SpriteFrame) => {
  62. if (!node.isValid) { return }
  63. if (err) {
  64. console.warn("加载资源失败:", url)
  65. if (needDelFirst == false) {
  66. node.getComponent(cc.Sprite).spriteFrame = null
  67. }
  68. callback(false)
  69. return
  70. }
  71. if (_tempImageTag != node['imageTag']) {
  72. return
  73. }
  74. if (this.resList[bundleName] == null) {
  75. this.resList[bundleName] = {}
  76. }
  77. this.resList[bundleName][url] = asset
  78. node.getComponent(cc.Sprite).spriteFrame = asset
  79. callback(true)
  80. })
  81. });
  82. }
  83. // 根据道具配置id加载道具图标
  84. static loadItemIcon(node: cc.Node, kindItem: KindItem, callback: Function = (success: boolean) => { }) {
  85. if (kindItem?.[0] == ItemKind.money) {
  86. this.loadTexture(node, IconUrl.item + GameDataCenter.item.getItemCfgBase(kindItem)?.icon, callback)
  87. } else {
  88. this.loadTexture(node, IconUrl.item + GameDataCenter.item.getItemCfgBase(kindItem)?.icon, callback)
  89. // console.error("loadItemIcon:未处理的道具类型:", kindItem[0])
  90. }
  91. }
  92. static spineList: { [bundleName: string]: { [key: string]: sp.SkeletonData } } = {}
  93. // 根据详细地址加载Spine资源
  94. static loadSpine(node: cc.Node, url: string, callback: Function = (success: boolean) => { }, needDelFirst: boolean = true) {
  95. if (node.getComponent(sp.Skeleton) == null) {
  96. node.addComponent(sp.Skeleton)
  97. }
  98. let bundleName = url.split("/")[0]
  99. if (this.spineList[bundleName] != null && this.spineList[bundleName][url] != null && this.spineList[bundleName][url].isValid && this.spineList[bundleName][url].loaded) {
  100. // if (this.spineList[bundleName][url].getTexture().isValid && this.spineList[bundleName][url].getTexture().loaded) {
  101. node.getComponent(sp.Skeleton).skeletonData = this.spineList[bundleName][url]
  102. callback(true)
  103. return
  104. // }
  105. }
  106. if (needDelFirst) {
  107. node.getComponent(sp.Skeleton).skeletonData = null
  108. }
  109. if (node['imageTag'] == null) {
  110. node['imageTag'] = 1
  111. } else {
  112. if (node['imageTag'] > 100) {
  113. node['imageTag'] = 1
  114. } else {
  115. node['imageTag'] += 1
  116. }
  117. }
  118. let _tempImageTag = node['imageTag']
  119. AssetsBundleMgr.loadBundle(bundleName, (error, bundle: cc.AssetManager.Bundle) => {
  120. if (error) {
  121. console.error(`bundle加载失败: ${bundleName}`);
  122. if (needDelFirst == false) {
  123. node.getComponent(cc.Sprite).spriteFrame = null
  124. }
  125. return;
  126. }
  127. let realUrl = url.slice(url.indexOf("/") + 1)
  128. bundle.load(realUrl, sp.SkeletonData, (err, asset: sp.SkeletonData) => {
  129. if (!node.isValid) { return }
  130. if (err) {
  131. console.warn("加载资源失败:", url)
  132. if (needDelFirst == false) {
  133. node.getComponent(sp.Skeleton).skeletonData = null
  134. }
  135. callback(false)
  136. return
  137. }
  138. if (_tempImageTag != node['imageTag']) {
  139. return
  140. }
  141. if (this.spineList[bundleName] == null) {
  142. this.spineList[bundleName] = {}
  143. }
  144. this.spineList[bundleName][url] = asset
  145. node.getComponent(sp.Skeleton).skeletonData = asset
  146. callback(true)
  147. })
  148. });
  149. }
  150. // // 根据详细地址获取图片资源
  151. // static getTexture(url: string, callback: Function = (success: boolean, asset: cc.SpriteFrame) => { }) {
  152. // let bundleName = url.split("/")[0]
  153. // if (this.resList[bundleName] != null && this.resList[bundleName][url] != null && this.resList[bundleName][url].isValid && this.resList[bundleName][url].loaded) {
  154. // if (this.resList[bundleName][url].getTexture().isValid && this.resList[bundleName][url].getTexture().loaded) {
  155. // callback(true, this.resList[bundleName][url])
  156. // }
  157. // }
  158. // AssetsBundleMgr.loadBundle(bundleName, (error, bundle: cc.AssetManager.Bundle) => {
  159. // if (error) {
  160. // console.error(`bundle加载失败: ${bundleName}`);
  161. // return;
  162. // }
  163. // let realUrl = url.slice(url.indexOf("/") + 1)
  164. // bundle.load(realUrl, cc.SpriteFrame, (err, asset: cc.SpriteFrame) => {
  165. // if (err) {
  166. // console.warn("加载资源失败:", url)
  167. // callback(false)
  168. // return null
  169. // }
  170. // if (this.resList[bundleName] == null) {
  171. // this.resList[bundleName] = {}
  172. // }
  173. // this.resList[bundleName][url] = asset
  174. // callback(true, this.resList[bundleName][url])
  175. // })
  176. // });
  177. // }
  178. static clearByBundle(bundleName: string) {
  179. if (this.resList[bundleName] != null) {
  180. this.resList[bundleName] = {}
  181. }
  182. }
  183. static clear() {
  184. this.resList = {}
  185. }
  186. }