/// <summary> /// 把某个页面入栈, 把某个页面显示在界面上 /// </summary> public void PushPanel(string uiname, bool colsable, object arg) { if (m_PanelStack == null) { m_PanelStack = new Stack <PanelBase>(); } PanelBase nextPanel = GetPanel(uiname); // 计划打开的页面 PanelBase currentPanel = null; // 最近一次关闭的界面 // 判断一下栈里面是否有页面 if (m_PanelStack.Count > 0) { PanelBase topPanel = m_PanelStack.Peek(); // 获取栈顶页面 // 如果即将打开的页面是栈顶页面,即关闭栈顶页面 if (topPanel == nextPanel) { if (colsable) { PopPanel(); } return; } // 当栈内有面板时,进行判断 while (m_PanelStack.Count > 0) { if (topPanel.Level < nextPanel.Level) { break; } // 如果栈顶页面的层级不小于要打开的页面层级,关闭它并保存 currentPanel = PopPanel(); if (m_PanelStack.Count > 0) { topPanel = m_PanelStack.Peek(); } } } // 如果最后关闭的界面和要打开的是同一个,就不打开了 if (currentPanel != nextPanel) { nextPanel.OnOpen(arg); m_PanelStack.Push(nextPanel); // 将打开的面板入栈 } }
/// <summary> /// 根据面板类型 得到实例化的面板 /// </summary> /// <returns></returns> public PanelBase GetPanel(string uiname) { if (m_PanelDict == null) { m_PanelDict = new Dictionary <string, PanelBase>(); } m_PanelDict.TryGetValue(uiname, out PanelBase panel); if (panel == null) { // 根据prefab去实例化面板 m_PanelPathDict.TryGetValue(uiname, out string path); Debug.Log(path); GameObject instPanel = GameObject.Instantiate(Resources.Load(path)) as GameObject; // UICore与派生类不一定在一个程序集类,所以不能直接用Type.GetType Assembly asmb = Assembly.Load("Assembly-CSharp"); Type type = asmb.GetType(uiname); if (type == null || type.IsSubclassOf(typeof(PanelBase))) { throw new FrameworkException("[UI] 面板类名错误 | 没有继承BasePanel"); } PanelBase basePanel = instPanel.AddComponent(type) as PanelBase; basePanel.Init(uiname); m_PanelDict.Add(uiname, basePanel); Transform uiGroup = CanvasTransform.Find("Level" + basePanel.Level); if (uiGroup == null) { RectTransform rect; rect = (new GameObject("Level" + basePanel.Level)).AddComponent <RectTransform>(); rect.SetParent(CanvasTransform); rect.sizeDelta = CanvasTransform.sizeDelta; rect.position = CanvasTransform.position; rect.localScale = Vector3.one; uiGroup = rect; } instPanel.transform.SetParent(uiGroup, false); return(basePanel); } else { return(panel); } }
/// <summary> /// 出栈 ,把页面从界面上移除 /// </summary> public PanelBase PopPanel() { if (m_PanelStack == null) { m_PanelStack = new Stack <PanelBase>(); } if (m_PanelStack.Count <= 0) { return(null); } PanelBase topPanel = m_PanelStack.Pop(); // 获取并移除栈顶面板 topPanel.OnClose(); // 关闭面板 return(topPanel); }
/// <summary> /// 获取面板 /// </summary> public PanelBase GetPanel(string uiname) { if (m_PanelDict.TryGetValue(uiname, out PanelBase panel)) { if (panel == null) { throw new XFrameworkException("[UI] The panel you want has been unloaded"); } return(panel); } else { // 根据prefab去实例化面板 m_PanelPathDict.TryGetValue(uiname, out string path); GameObject instPanel = GameObject.Instantiate(m_ResMgr.Load <GameObject>(path)); // UICore与派生类不一定在一个程序集类,所以不能直接用Type.GetType Assembly asmb = Assembly.Load("Assembly-CSharp"); Type type = asmb.GetType(uiname); if (type == null || !type.IsSubclassOf(typeof(PanelBase))) { throw new XFrameworkException("[UI] wrong panel name or panel is not inherit BasePanel"); } PanelBase basePanel = instPanel.AddComponent(type) as PanelBase; basePanel.Init(uiname); m_PanelDict.Add(uiname, basePanel); Transform uiGroup = CanvasTransform.Find("Level" + basePanel.Level); if (uiGroup == null) { RectTransform rect; rect = (new GameObject("Level" + basePanel.Level)).AddComponent <RectTransform>(); int siblingIndex = CanvasTransform.childCount; for (int i = 0, length = CanvasTransform.childCount; i < length; i++) { string levelName = CanvasTransform.GetChild(i).name; if (int.TryParse(levelName[levelName.Length - 1].ToString(), out int level)) { if (basePanel.Level < level) { siblingIndex = i; break; } } } rect.SetParent(CanvasTransform); rect.SetSiblingIndex(siblingIndex); rect.sizeDelta = CanvasTransform.GetComponent <UnityEngine.UI.CanvasScaler>().referenceResolution; rect.anchorMin = Vector2.zero; rect.anchorMax = Vector2.one; rect.anchoredPosition3D = Vector3.zero; rect.sizeDelta = Vector2.zero; rect.localScale = Vector3.one; uiGroup = rect; } instPanel.transform.SetParent(uiGroup, false); return(basePanel); } }
internal void Init(Transform transform, PanelBase panel) { this.transform = transform; this.panel = panel; }
internal void Config(PanelBase parentPanel) { this.parentPanel = parentPanel; }