public void ClosePanel(string name) { PanelBase panel = (PanelBase)panelDict[name]; if (panel == null) { return; } panel.OnClosing(); panelDict.Remove(name); panel.OnClosed(); GameObject.Destroy(panel.skin); //panel.skin.SetActive(false); Component.Destroy(panel); //panel.enabled = false; }
//一个脚本只能存在一个实例 //一个脚本可以使用不同面板资源,skinPath可以临时自定义面板资源 public void OpenPanel <T>(string skinPath = "", params object[] args) where T : PanelBase { //检查面板实例是否已经创建,这样的话,一个面板在游戏中只能打开一个? //string name = typeof(T).ToString(); string name = typeof(T).Name; if (panelDict.ContainsKey(name)) { PanelBase oldPanel = panelDict[name]; //oldPanel.enabled = true; //和GameObject的SetActive有什么区别???作用对象不同 //oldPanel.skin.SetActive(true); int count = oldPanel.skin.transform.parent.childCount; oldPanel.skin.transform.SetSiblingIndex(count - 1); return; } //创建面板实例 PanelBase newPanel = canvas.AddComponent <T>(); newPanel.Init(args); panelDict.Add(name, newPanel); //为面板实例创建面板资源 skinPath = (skinPath != "" ? skinPath : newPanel.skinPath); GameObject skin = Resources.Load <GameObject>(skinPath); if (skin == null) { Debug.LogError("panelMgr.OpenPanel fail, skin is null,skinPath = " + skinPath); } newPanel.skin = (GameObject)Instantiate(skin); //设置面板资源层级 Transform skinTrans = newPanel.skin.transform; PanelLayer layer = newPanel.layer; Transform parent = layerDict[layer]; skinTrans.SetParent(parent, false); //自定义 newPanel.OnShowing(); newPanel.OnShowed(); }