1. 程式人生 > >UI框架搭建DAY2

UI框架搭建DAY2

今天的主要任務是完善NormalPanel, 搭建PopupPanel。

在編寫PanelManager的過程中,發現了一個bug。昨天把panelPath直接傳給了ResourceManager.GetInstance().LoadAsset<GameObject>(path);

今天做了修改,並且添加了初始化dictPanelPath的方法,為此在SysDefine新添加了一個類,PanelNameStr。在Helper類裡添加了以一個方法GetPathByName。

 

部分修改程式碼如下,偷了個懶,直接全部複製貼上了:

PanelManager.cs

  1
/* 2 2018.12.30修改記錄:1.增加了SetDictPanelPath()方法 3 2.修改了CreatePanel()方法 4 3.增加了DestroyPanel()方法 5 */ 6 using System.Collections.Generic; 7 using UnityEngine; 8 public class PanelManager 9 { 10 //本類例項 11 private static PanelManager _instance;
12 //儲存面板名字和對應的路徑字典 13 public static Dictionary<string, string> dictPanelPath; 14 //儲存已顯示的面板字典 15 public static Dictionary<string, BasePanel> dictCurPanel; 16 //儲存已隱藏的面板字典 17 public static Dictionary<string, BasePanel> dictHidePanel; 18 //儲存Popup型別面板的字典 19
public static Dictionary<string, Stack<BasePanel>> dictPopupPanel; 20 21 //單例模式 22 private PanelManager() { } 23 public static PanelManager GetInstance() 24 { 25 if(_instance == null) 26 { 27 _instance = new PanelManager(); 28 29 InitProperties(); 30 SetDictPanelPath(); 31 } 32 return _instance; 33 } 34 //初始化欄位 35 private static void InitProperties() 36 { 37 dictPanelPath = new Dictionary<string, string>(); 38 dictCurPanel = new Dictionary<string, BasePanel>(); 39 dictHidePanel = new Dictionary<string, BasePanel>(); 40 dictPopupPanel = new Dictionary<string, Stack<BasePanel>>(); 41 } 42 private static void SetDictPanelPath() 43 { 44 dictPanelPath.Add(PanelNameStr.LogOnPanel, PrefabPathStr.logOnPanelPath); 45 dictPanelPath.Add(PanelNameStr.RegisterPanel, PrefabPathStr.registerPanelPath); 46 } 47 /// <summary> 48 /// 建立一個面板 49 /// 先檢查dictHidePanel集合裡是否存在此面板,有則取出,顯示,並加入dictCurPanel集合 50 /// 沒有,則建立一個,然後加如dictCurPanel集合。 51 /// </summary> 52 /// <param name="panelName">要建立的面板的名字</param> 53 /// <returns></returns> 54 public BasePanel CreatePanel(string panelName) 55 { 56 BasePanel basePanel = null; 57 dictHidePanel.TryGetValue(panelName, out basePanel); 58 if(basePanel != null) 59 { 60 basePanel.Open(); 61 //新增到正在顯示的面板集合 62 dictCurPanel.Add(panelName, basePanel); 63 return basePanel; 64 } 65 else 66 { 67 68 string path = Helper.GetInstance().GetPathByName(panelName); 69 70 //根據儲存路徑,載入預製體 71 GameObject go = ResourceManager.GetInstance().LoadAsset<GameObject>(path); 72 if(go != null) 73 { 74 basePanel = go.GetComponent<BasePanel>(); 75 if(basePanel != null) 76 { 77 //新增到正在顯示的面板集合 78 dictCurPanel.Add(panelName, basePanel); 79 } 80 else 81 { 82 Debug.LogError(GetType()+"你可能忘記掛載了BasePanel型別的指令碼"); 83 } 84 return basePanel; 85 } 86 else 87 { 88 Debug.Log(GetType()+"請檢查配置檔案,預製體不存在"); 89 90 } 91 } 92 return null; 93 } 94 95 /// <summary> 96 /// 1.從dictCurPanel集合中取出對應的面板 97 /// 2.隱藏 98 /// 3.加入dictHidePanel集合 99 /// </summary> 100 /// <param name="PanelName"></param> 101 public void DestroyPanel(string panelName) 102 { 103 BasePanel basePanel = null; 104 basePanel = dictCurPanel[panelName]; 105 if(basePanel == null) 106 { 107 Debug.LogError(GetType()+"面板不存在,請檢查配置檔案"); 108 return; 109 } 110 else 111 { 112 //關閉面板 113 basePanel.Close(); 114 //加入dictHidePanel集合 115 dictHidePanel.Add(panelName, basePanel); 116 } 117 } 118 119 }

為了測試DestroyPanel,新建了一個面板RegisterPanel。

在LogOnPanel.cs類裡測試

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class LogOnPanel : BasePanel
 6 {
 7     private void Awake()
 8     {
 9         this.panelType = EPanelType.Normal;
10     }
11 
12     
13     public void OnStartGameBtnClick()
14     {
15         //TO DO
16     }
17     //測試DestroyPanel()方法
18     public void OnRegisterBtnClick()
19     {
20         //隱藏自身
21         PanelManager.GetInstance().DestroyPanel(PanelNameStr.LogOnPanel);
22         //顯示註冊面板
23         PanelManager.GetInstance().CreatePanel(PanelNameStr.RegisterPanel);
24     }
25 
26 }

另外在設計過程中和最初的想法有些出入,本來打算用工廠方法模式成產各種型別的Panel,現在型別已經在Awake方法裡註冊了,而且也不麻煩,所以把NormalPanel.cs,PopupPanel.cs,HideOtherPanel.cs刪除了,我要去學習Shader了,下午繼續更PopupPanel的搭建。