57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import { _decorator, Node, director, resources, instantiate, Canvas, Component, js } from 'cc';
|
|
import { PanelBase } from './PanelBase';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
@ccclass('UIManager')
|
|
export class UIManager extends Component {
|
|
static instance: UIManager
|
|
|
|
|
|
|
|
private panels: Map<string, PanelBase> = new Map();
|
|
|
|
@property({ type: Node })
|
|
uiRoot: Node = null;
|
|
|
|
onLoad() {
|
|
UIManager.instance = this;
|
|
}
|
|
|
|
|
|
// 打开UI面板
|
|
public async openPanel<T extends PanelBase>(panelClass: new () => T): Promise<T> {
|
|
const panelName = js.getClassName(panelClass);
|
|
|
|
if (!this.panels.has(panelName)) {
|
|
let panelNode = PanelBase.LoadPanel(`ui/${js.getClassName(panelClass)}`, this.uiRoot)
|
|
let newPanel = (await panelNode).getComponent(PanelBase)
|
|
this.panels.set(panelName, newPanel);
|
|
}
|
|
const existingPanel = this.panels.get(panelName);
|
|
if (existingPanel) {
|
|
existingPanel.open();
|
|
return existingPanel as T;
|
|
}
|
|
|
|
}
|
|
// 关闭UI面板
|
|
public closePanel<T extends PanelBase>(panelClass: new () => T): void {
|
|
const panelName = js.getClassName(panelClass);
|
|
if (this.panels.has(panelName)) {
|
|
const panel = this.panels.get(panelName);
|
|
if (panel) {
|
|
panel.close();
|
|
this.panels.delete(panelName);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 关闭所有面板
|
|
public closeAllPanels(): void {
|
|
this.panels.forEach(panel => {
|
|
console.log("关闭页面", panel.node.name)
|
|
panel.close();
|
|
});
|
|
this.panels.clear();
|
|
}
|
|
} |