123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- import { Constructor, director, error, find, instantiate, Node, Prefab, resources, Scene } from "cc";
- import { UIBase, UIType } from "../Game/GameFrameWork/UIBase";
- class UIManager {
- static instance: UIManager = null;
- private _uis: Map<string, UIBase> = new Map();
- private _uiNodes: Map<string, Node> = new Map();
- private _prefabs: Map<string, Prefab> = new Map();
-
- getUI<T extends UIBase>(name: string | Constructor<T>): T {
- if (typeof (name) === "string") {
- return <T>this._uis.get(name);
- }
-
- for (const ui of Array.from(this._uis.values())) {
- if (ui instanceof name) {
- return <T>ui;
- }
- }
- return null;
- }
-
- async openUI(preName: string, type: UIType = UIType.PAGE) {
- let ui = this._uis.get(preName);
- if (ui) {
-
- ui.show();
- return;
- }
-
- let node = this._uiNodes.get(preName);
- if (node) {
- node.active = true;
- return;
- }
-
-
- let prefab = this._prefabs.get(preName);
- if (!prefab) {
- prefab = await this._loadUI("UI/" + preName);
- this._prefabs.set(preName, prefab)
- }
-
- node = instantiate(prefab);
- let root = find("Canvas/UIRoot");
- if(!root){
-
- const rootPre: Prefab = await this._loadUI("UI/UIRoot")
- root = instantiate(rootPre);
- root.parent = find("Canvas");
- }
- node.parent = root.children[type];
-
- ui = node.getComponent(UIBase);
- if (ui) {
-
- ui.init();
- ui.UIName = preName;
- this._uis.set(preName, ui);
- return;
- }
- this._uiNodes.set(preName, node);
-
-
-
-
-
-
-
- }
- async openScene(sceneName: string, uiName?: string){
- await this._loadScene(sceneName);
- uiName && this.openUI(uiName);
- }
- private _loadScene(sceneName: string){
- new Promise((revolve, reject)=>{
- director.loadScene(sceneName,(err: Error, scene: Scene)=>{
- if(err){
- error("加载" + sceneName + "场景错误!");
- reject(err);
- }
- else{
- revolve(scene);
- }
- })
- })
- }
-
- private _loadUI(path: string): Promise<Prefab> {
- return new Promise((revole, reject) => {
- resources.load(path, Prefab, (err: Error, asset: Prefab) => {
- if (err) {
- reject(err);
- } else {
- revole(asset);
- }
- })
- })
- }
-
- closeUI(uibase: string | UIBase, clear: boolean = true){
- let ui: UIBase;
- if(typeof(uibase) === "string"){
- ui = this._uis.get(uibase);
- if(ui){
- ui.hide(clear);
- clear && this._uis.delete(uibase);
- }
- return;
- }
-
- uibase.hide(clear);
- clear && this._uis.delete(uibase.UIName);
- return;
-
-
-
-
-
-
- }
-
- sendMsg(msg: string,ui: string|UIBase, ...args){
- let m: UIBase;
- if(typeof(ui) === "string"){
- m = this.getUI(ui);
- }
- else{
- m = ui;
- }
-
- if(!m){
- return;
- }
-
-
- const func = m[msg];
- if(func && typeof(func) === "function"){
- func.apply(m, [...args]);
- return;
- }
- }
- }
- export const UIMgr: UIManager = UIManager.instance = new UIManager();
|