LayerMgr.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { _decorator, assetManager, AssetManager, director, instantiate, Label, Node, Prefab, sp, Sprite, SpriteFrame, tween, Vec3 } from 'cc';
  2. import { BundleName } from '../Config/EnumCfg';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('LayerMgr')
  5. export class LayerMgr {
  6. private static _layerMgr: LayerMgr = null;
  7. private _dialogList: any = null;
  8. public static get instance(): LayerMgr {
  9. if (this._layerMgr == null) {
  10. this._layerMgr = new LayerMgr()
  11. }
  12. return this._layerMgr;
  13. }
  14. private constructor() {
  15. this._dialogList = {}
  16. }
  17. loadBundle(bundleName: string, callback?: Function) {
  18. if (!bundleName) return;
  19. let bundle: AssetManager.Bundle = this.getBundle(bundleName)
  20. if (bundle) {
  21. // 关卡包先预加载动画和声音资源
  22. if (bundleName.indexOf("Unit") > -1) {
  23. this.preloadDir(bundle, "Animation")
  24. this.preloadDir(bundle, "Audio")
  25. }
  26. if (callback)
  27. callback(bundle)
  28. return;
  29. }
  30. AssetManager.instance.loadBundle(bundleName, AssetManager.Bundle, (err, res) => {
  31. if (err) {
  32. console.log(`load ${bundleName} bundle err: `, err)
  33. return;
  34. }
  35. console.log(`load ${bundleName} complete`, bundleName.indexOf("Unit"))
  36. // 关卡包先预加载动画和声音资源
  37. if (bundleName.indexOf("Unit") > -1) {
  38. this.preloadDir(res, "Animation")
  39. this.preloadDir(res, "Audio")
  40. }
  41. if (callback)
  42. callback(res)
  43. })
  44. }
  45. changeScene(bundleName: string, sceneName: string, callback?: any) {
  46. let bundle: AssetManager.Bundle = this.getBundle(bundleName)
  47. if (bundle) {
  48. this.loadScene(bundle, sceneName, (res) => {
  49. director.loadScene(sceneName, callback);
  50. });
  51. } else {
  52. this.loadBundle(bundleName, (res) => {
  53. this.loadScene(res, sceneName, (scene) => {
  54. director.loadScene(sceneName, callback);
  55. })
  56. })
  57. }
  58. }
  59. preloadDir(bundle: AssetManager.Bundle, dir: string) {
  60. console.log("start preloaddir: ", bundle.name, dir)
  61. if (!bundle || !dir) return
  62. bundle.preloadDir(dir, (err, res) => {
  63. if (err)
  64. console.log("preloaddir err")
  65. else
  66. console.log("preloaddir complete: ", bundle.name, dir)
  67. })
  68. }
  69. private loadScene(bundle: AssetManager.Bundle, sceneName: string, callback?: Function) {
  70. if (!bundle || !sceneName) return;
  71. bundle.loadScene(sceneName, (err, res) => {
  72. if (err) {
  73. console.error(`load ${sceneName} scene err: `)
  74. return;
  75. }
  76. if (callback)
  77. callback(res)
  78. })
  79. }
  80. showMsgTips(str: string, time?) {
  81. let msgTips = null;
  82. let bundle: AssetManager.Bundle = this.getBundle(BundleName.Res)
  83. if (!bundle) return;
  84. bundle.load("Prefab/msgTips", Prefab, (err, res) => {
  85. if (err) {
  86. console.error("load msgTips err", err);
  87. return;
  88. }
  89. msgTips = instantiate(res);
  90. msgTips.getChildByName("label").getComponent(Label).string = str
  91. // msgTips.scale = 1;
  92. director.getScene().getChildByName("Canvas").addChild(msgTips);
  93. console.log("show msgTips: ", str)
  94. msgTips.scale = Vec3.ZERO
  95. tween(msgTips).to(0.3, { scale: Vec3.ONE }, { easing: "sineIn" }).delay(1).to(0.3, { opacity: 0 }, { easing: "fade" }).delay(time ? time : 0).call(() => {
  96. msgTips.removeFromParent()
  97. msgTips.destroy()
  98. }).start()
  99. })
  100. }
  101. ShowPrefab(bundleName: BundleName, prefab: string, call?: Function) {
  102. let bundle: AssetManager.Bundle = this.getBundle(bundleName)
  103. if (bundle) {
  104. bundle.load(prefab, Prefab, (err, res) => {
  105. if (err) {
  106. console.log(`load ${prefab} err: `, bundleName);
  107. return
  108. }
  109. let node: Node = instantiate(res as Prefab);
  110. director.getScene().getChildByName('Canvas').addChild(node);
  111. if (call)
  112. call(node)
  113. })
  114. }
  115. }
  116. ShowPrefab2(bundleName: BundleName, prefab: string, call?: Function) {
  117. let bundle: AssetManager.Bundle = this.getBundle(bundleName)
  118. if (bundle) {
  119. bundle.load(prefab, Prefab, (err, res) => {
  120. if (err) {
  121. console.log(`load ${prefab} err: `, bundleName);
  122. return
  123. }
  124. let node: Node = instantiate(res as Prefab);
  125. director.getScene().getChildByName('Canvas').addChild(node);
  126. if (call)
  127. call(node)
  128. })
  129. }
  130. }
  131. ShowSpine(bundlename: string, url: string, spine: sp.Skeleton, cb: Function = null) {
  132. let subres = assetManager.getBundle(bundlename)
  133. if (subres) {
  134. subres.load(url, sp.SkeletonData, (error: Error, sp: sp.SkeletonData) => {
  135. if (sp != null) {
  136. if (cb) {
  137. spine.skeletonData = sp
  138. cb(sp);
  139. } else {
  140. spine.skeletonData = sp
  141. }
  142. }
  143. else {
  144. alert("err:" + error);
  145. }
  146. })
  147. }
  148. }
  149. ShowSprite(bundleName: BundleName | string, spritepath: string, changeSpriteFrameNode: Node, call?: Function) {
  150. let bundle: AssetManager.Bundle = this.getBundle(bundleName)
  151. if (bundle) {
  152. bundle.load(spritepath + "/spriteFrame", SpriteFrame, (err, res) => {
  153. if (err) {
  154. console.error(err)
  155. console.log(`load ${spritepath} err: `, bundleName);
  156. return
  157. }
  158. let spr: SpriteFrame = (res as SpriteFrame);
  159. changeSpriteFrameNode.getComponent(Sprite).spriteFrame = spr;
  160. call && call(spr)
  161. })
  162. }
  163. }
  164. ShowSprite2(bundleName: BundleName | string, spritepath: string, spriter: Sprite, call?: Function) {
  165. let bundle: AssetManager.Bundle = this.getBundle(bundleName)
  166. if (bundle) {
  167. bundle.load(spritepath + "/spriteFrame", SpriteFrame, (err, res) => {
  168. if (err) {
  169. console.error(err)
  170. console.log(`load ${spritepath} err: `, bundleName);
  171. return
  172. }
  173. let spr: SpriteFrame = (res as SpriteFrame);
  174. spriter.spriteFrame = spr;
  175. call && call(spr)
  176. })
  177. }
  178. }
  179. getBundle(bundleName: string): AssetManager.Bundle {
  180. if (bundleName == BundleName.Res) return assetManager.resources
  181. return assetManager.getBundle(bundleName)
  182. }
  183. }