UISystem.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. const { ccclass, property } = cc._decorator;
  2. @ccclass
  3. /**
  4. * UI管理类
  5. * @author 冯聪
  6. */
  7. export default class UISystem extends cc.Component {
  8. /** panel父节点 */
  9. private panelParent: cc.Node = null;
  10. /** 是否正在打开界面 */
  11. private isPanelOpening: boolean = false;
  12. /** 是否界面正在关闭 */
  13. private isPanelClosing: boolean = false;
  14. /** 当前打开的UIPanel */
  15. private curOnPanelDic: { [key: string]: cc.Node } = {};
  16. /** 上一个打开的界面名称 */
  17. private lastOpenPanelName: string = "";
  18. onLoad() {
  19. mk.ui = this;
  20. }
  21. start() {
  22. if (!this.panelParent) {
  23. this.panelParent = this.node;
  24. }
  25. }
  26. /**
  27. * 打开panel界面
  28. * @param panelUrl 界面名称全路径
  29. * @param openAction 打开之后的操(需不需要关闭其他界面)
  30. */
  31. openPanel(panelUrl: string, openAction: OpenActionType = OpenActionType.normal, parent: cc.Node = null): Promise<any> {
  32. let arr = panelUrl.split("/");
  33. let panelName = arr[arr.length - 1];
  34. let self = this;
  35. if (self.isPanelOpening || self.getCurOnPanel(panelName)) {
  36. return;
  37. }
  38. self.isPanelOpening = true;
  39. return new Promise((resolve, reject) => {
  40. mk.loader.load(`${panelUrl}`, cc.Prefab)
  41. .then((prefab) => {
  42. let node_panel = cc.instantiate(prefab);
  43. self.curOnPanelDic[panelName] = node_panel;
  44. if (self.panelParent) {
  45. self.panelParent = self.node;
  46. }
  47. //如果父节点不存在,就默认把该节点定为panel的父节点
  48. if (!self.panelParent) {
  49. console.error("[UISystem] UI的父节点panelParent为null,请检查脚本挂载情况");
  50. self.isPanelOpening = false;
  51. return;
  52. }
  53. if (parent) {
  54. node_panel.parent = parent;
  55. }
  56. else {
  57. self.panelParent.addChild(node_panel);
  58. }
  59. switch (openAction) {
  60. case OpenActionType.normal:
  61. break;
  62. case OpenActionType.closeLast:
  63. if (self.lastOpenPanelName) {
  64. self.closePanel(self.lastOpenPanelName);
  65. }
  66. break;
  67. case OpenActionType.closeOther:
  68. let keys = Object.keys(self.curOnPanelDic).filter((key) => { key != panelName });
  69. for (var i = 0; i < keys.length; i++) {
  70. let key = keys[i];
  71. self.closePanel(key);
  72. }
  73. break;
  74. }
  75. self.lastOpenPanelName = panelName;
  76. //返回panel
  77. resolve(node_panel);
  78. self.isPanelOpening = false;
  79. })
  80. .catch((err) => {
  81. self.isPanelOpening = false;
  82. reject(err);
  83. })
  84. });
  85. }
  86. /**
  87. * 获取当前打开的panel界面
  88. * @param panelName 界面名称
  89. */
  90. getCurOnPanel(panelName: string): cc.Node {
  91. return this.curOnPanelDic[panelName];
  92. }
  93. /**
  94. * 关闭panel界面
  95. * @param panelName 通常使用this.node.name代替,界面名称,不是全路径
  96. * @param callBack 关闭之后回调
  97. */
  98. closePanel(panelName: string, callBack: Function = null) {
  99. if (this.isPanelClosing || !this.getCurOnPanel(panelName)) {
  100. return;
  101. }
  102. this.isPanelClosing = true;
  103. let node_panel = this.getCurOnPanel(panelName);
  104. // 是否存在关闭动画
  105. let comp_anim = node_panel.getComponent('EffectOpenAndClose');
  106. if (comp_anim) {
  107. comp_anim.hideEffect(this.destroyNode.bind(this, node_panel, panelName, callBack));
  108. } else {
  109. this.destroyNode(node_panel, panelName, callBack);
  110. }
  111. mk.event.emit('close-panel', panelName);
  112. }
  113. private destroyNode(node_panel, panelName, callBack = null) {
  114. node_panel.destroy();
  115. delete this.curOnPanelDic[panelName];
  116. this.isPanelClosing = false;
  117. callBack && callBack();
  118. }
  119. /**
  120. * 关闭所有界面,除了主界面
  121. * @param except 排除的ui
  122. */
  123. closeAllUI(except: string[] = ["game", "guide"]) {
  124. for (let panelName in this.curOnPanelDic) {
  125. if (except && except.length > 0 && except.indexOf(panelName) != -1) {
  126. continue;
  127. }
  128. let node_panel = this.getCurOnPanel(panelName);
  129. let comp_anim = node_panel.getComponent('EffectOpenAndClose');
  130. if (comp_anim) {
  131. comp_anim.hideEffect(this.destroyNode.bind(this, node_panel, panelName));
  132. } else {
  133. this.destroyNode(node_panel, panelName);
  134. }
  135. mk.event.emit('close-panel', panelName);
  136. }
  137. this.isPanelClosing = false;
  138. mk.ad.destroyNativeAd()
  139. mk.ad.destoryBanner()
  140. }
  141. }
  142. /** 界面打开操作 */
  143. export enum OpenActionType {
  144. /** 普通不操作 */
  145. normal = 0,
  146. /** 关闭其他 */
  147. closeOther = 1,
  148. /** 关闭上一个 */
  149. closeLast = 2
  150. }