import { _decorator, Button, Component, Node, PageView } from 'cc'; const { ccclass, property } = _decorator; export enum UIType { PAGE, WIDGET, POPWIN, } @ccclass('UIBase') export class UIBase extends Component { protected _nodes: Map = new Map(); protected _uiName: string = "ui"; get UIName() { return this._uiName; } set UIName(name: string) { this._uiName = name; } //如果使用ui都要进行相同的初始化 init() { this._visit(this.node) //多态 this.onStart(); } //各个ui都有不同的初始化行为 protected onStart() { } private _visit(node: Node) { //如果这个节点的名字是 “ _ ” 开头的 就是需要操作的节点 if (node.name.startsWith("_")) { this._nodes.set(node.name, node); } for (const child of node.children) { this._visit(child); } } show() { this.node.active = true; this.use(); } showing() { return this.node.active; } //true -> 销毁 false -> 不销毁 默认销毁 hide(clear: Boolean = true) { this.unUse(); if (clear) { this.node.destroy(); return; } this.node.active = false; } //多态 //用它的时候 protected use() { } //不用它的时候 protected unUse() { } //UI 事件 //按钮点击 onBtnClick(name: string, callback: Function, ...arg) { const node = this._nodes.get(name); if (node) { node.on(Button.EventType.CLICK, () => { //apply指定函数的this指向 并且立即执行 //参数1 就是函数执行过程中的this指向 callback.apply(this, [...arg]); }) } } //pageView的事件 onPageView(name: string, type, callback: Function) { this._nodes.get(name).on(type, callback); } getPageView(name: string): PageView { return this._nodes.get(name).getComponent(PageView); } getNode(name: string): Node { return this._nodes.get(name); } onMsg(msg: string, callback: Function) { } }