using System.Collections.Generic; using Unity.VisualScripting.FullSerializer; using UnityEngine;
public class UIManager { public static UIManager instance = new UIManager(); public static UIManager Instance => instance; private Transform canvasTrans;
//字典存储 private Dictionary<string,BasePanel> panelDic = new Dictionary<string,BasePanel>(); //显示面板 public T ShowPanel<T>() where T : BasePanel { //获取面板名称 string panelName = typeof(T).Name; //检测是不是有了 if(panelDic.ContainsKey(panelName) ) return panelDic[panelName] as T; //如果没有我们就实例化一个UI GameObject panelObj = GameObject.Instantiate(Resources.Load<GameObject>("UI" + panelName)); //把这个对象放到场景Canvas下边 panelObj.transform.SetParent(canvasTrans, false); //获取这个UI上的脚本 T panel = panelObj.GetComponent<T>(); //把这个脚本存在字典里 panelDic.Add(panelName, panel); panel.Showme(); return panel; } //UIManager的构造函数 public UIManager() { //得到场景的Canvas对象 GameObject canvas = GameObject.Instantiate(Resources.Load<GameObject>("UI/Canvas")); canvasTrans = canvas.transform; GameObject.DontDestroyOnLoad(canvas); }