const { ccclass, property } = cc._decorator; @ccclass /** * UI管理类 * @author 冯聪 */ export default class UISystem extends cc.Component { /** panel父节点 */ private panelParent: cc.Node = null; /** 是否正在打开界面 */ private isPanelOpeningArr = []; /** 是否界面正在关闭 */ private isPanelClosingArr = []; /** 当前打开的UIPanel */ private curOnPanelDic: { [key: string]: cc.Node } = {}; /** 上一个打开的界面名称 */ private lastOpenPanelName: string = ""; onLoad() { mk.ui = this; } start() { if (!this.panelParent) { this.panelParent = this.node; } } /** * 打开panel界面 * @param panelUrl 界面名称全路径 * @param openAction 打开之后的操(需不需要关闭其他界面) */ openPanel(panelUrl: string, openAction: OpenActionType = OpenActionType.normal, parent: cc.Node = null): Promise { let arr = panelUrl.split("/"); let panelName = arr[arr.length - 1]; let self = this; if (self.isPanelOpeningArr.indexOf(panelName) != -1 || self.getCurOnPanel(panelName)) { return; } self.isPanelOpeningArr.push(panelName); return new Promise((resolve, reject) => { mk.loader.load(`${panelUrl}`, cc.Prefab) .then((prefab) => { let node_panel = cc.instantiate(prefab); self.curOnPanelDic[panelName] = node_panel; if (self.panelParent) { self.panelParent = self.node; } //如果父节点不存在,就默认把该节点定为panel的父节点 if (!self.panelParent) { console.error("[UISystem] UI的父节点panelParent为null,请检查脚本挂载情况"); let index = self.isPanelOpeningArr.indexOf(panelName); self.isPanelOpeningArr.splice(index, 1); return; } if (parent) { node_panel.parent = parent; } else { self.panelParent.addChild(node_panel); } switch (openAction) { case OpenActionType.normal: break; case OpenActionType.closeLast: if (self.lastOpenPanelName) { self.closePanel(self.lastOpenPanelName); } break; case OpenActionType.closeOther: let keys = Object.keys(self.curOnPanelDic).filter((key) => { return key != panelName }); for (var i = 0; i < keys.length; i++) { let key = keys[i]; self.closePanel(key); } break; } self.lastOpenPanelName = panelName; //返回panel resolve(node_panel); let index = self.isPanelOpeningArr.indexOf(panelName); self.isPanelOpeningArr.splice(index, 1); }) .catch((err) => { let index = self.isPanelOpeningArr.indexOf(panelName); self.isPanelOpeningArr.splice(index, 1); reject(err); }) }); } /** * 获取当前打开的panel界面 * @param panelName 界面名称 */ getCurOnPanel(panelName: string): cc.Node { return this.curOnPanelDic[panelName]; } /** * 关闭panel界面 * @param panelName 通常使用this.node.name代替,界面名称,不是全路径 * @param callBack 关闭之后回调 */ closePanel(panelName: string, callBack: Function = null) { if (this.isPanelClosingArr.indexOf(panelName) != -1 || !this.getCurOnPanel(panelName)) { return; } this.isPanelClosingArr.push(panelName); let node_panel = this.getCurOnPanel(panelName); if (!node_panel) { return; } // 是否存在关闭动画 let comp_anim = node_panel.getComponent('EffectOpenAndClose'); if (comp_anim && comp_anim.isOpenCloseEffect) { comp_anim.hideEffect(this.destroyNode.bind(this, node_panel, panelName, callBack)); } else { this.destroyNode(node_panel, panelName, callBack); } mk.event.emit('close-panel', panelName); } private destroyNode(node_panel, panelName, callBack = null) { node_panel.destroy(); delete this.curOnPanelDic[panelName]; let index = this.isPanelClosingArr.indexOf(panelName); this.isPanelClosingArr.splice(index, 1); callBack && callBack(); } /** * 关闭所有界面,除了主界面 * @param except 排除的ui */ closeAllUI(except: string[] = ["game", "guide"]) { for (let panelName in this.curOnPanelDic) { if (this.isPanelClosingArr.indexOf(panelName) != -1) { continue; } this.isPanelClosingArr.push(panelName); if (except && except.length > 0 && except.indexOf(panelName) != -1) { continue; } let node_panel = this.getCurOnPanel(panelName); if (!node_panel) { continue; } let comp_anim = node_panel.getComponent('EffectOpenAndClose'); if (comp_anim) { comp_anim.hideEffect(this.destroyNode.bind(this, node_panel, panelName)); } else { this.destroyNode(node_panel, panelName); } mk.event.emit('close-panel', panelName); } mk.ad.destroyNativeAd() mk.ad.destoryBanner() } /** 隐藏界面,不销毁 */ hideUI(node_panel, callBack: Function = null) { // 是否存在关闭动画 let comp_anim = node_panel.getComponent('EffectOpenAndClose'); if (comp_anim) { comp_anim.hideEffect(callBack); } else { node_panel.active = false; } } } /** 界面打开操作 */ export enum OpenActionType { /** 普通不操作 */ normal = 0, /** 关闭其他 */ closeOther = 1, /** 关闭上一个 */ closeLast = 2 }