UI框架 可以直接當一個小外掛的使用
1.資料夾規範:
2.做UI的時候:2D模式,正交攝像機
3.
選擇這個 數值改成0.5適應寬高
4.。一個空的image來當爹,其他的物體是他兒子,這樣兒子依附於他在右下角 就不會改變形狀了
5.方便ui管理,點選什麼,就出現什麼,用列舉
列舉和結構體都是值型別:
通過文字載入,通過單利進行,
通過Json加載出來,根據名字加載出來
json不能解析列舉,字串轉換列舉:
不解析列舉的屬性
讀取jison要序列化
存入json與解析json
用list存入:
list<i****nfo>
6.json文件的書寫(建立一個text文字來書寫),然後在校驗:
{}物件
:引出物件的集合
[]物件集合
文件內容:
在json校驗
程式碼部分(不是完整版本):
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using System; 5 [Serializable] 6 public class UIPanelInfo :ISerializationCallbackReceiver { 7 [NonSerialized]//不序列化(json解析不了列舉)8 public UIPanelType type; 9 public string path; 10 public string UIPanelTypeString; 11 12 public void OnAfterDeserialize()//在序列化之後進行轉換 13 { 14 type=(UIPanelType)Enum.Parse(typeof(UIPanelType),UIPanelTypeString); 15 } 16 17 public void OnBeforeSerialize() 18 { 19throw new NotImplementedException(); 20 } 21 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 [System.Serializable] 5 public class UIListFromJson { 6 7 public List<UIPanelInfo> ListInfo; 8 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public enum UIPanelType 6 { 7 Task, 8 Bag, 9 System, 10 Shop, 11 Skill, 12 ItemMessage, 13 UIMain 14 }
做什麼管理類 都做成單列
以文字檔案的形式進行載入。
通過返回父類 得到上邊圖片 到東西,採用虛方法:用虛方法不用全部實現,可以實列化,抽象方法的作用
名字要高度一致
暫停的方式:
最後(完整)的程式碼:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class MainPanel : BasePanel { 6 CanvasGroup ag; 7 // Use this for initialization 8 void Start () { 9 ag = GetComponent<CanvasGroup>(); 10 } 11 12 public override void OnPause() 13 { 14 ag.blocksRaycasts = false; 15 } 16 public override void OnContinue() 17 { 18 ag.blocksRaycasts = true; 19 } 20 public void OnClick(string type) 21 { 22 UIPanelType uI = (UIPanelType)System.Enum.Parse(typeof(UIPanelType),type); 23 UImanger.Instance.PushStack(uI); 24 25 } 26 27 }
第二個指令碼:
1 齊培良老師 2018/10/15 15:45:43 2 using System.Collections; 3 using System.Collections.Generic; 4 using UnityEngine; 5 6 public class UImanger 7 { 8 private static UImanger _instance; 9 public static UImanger Instance 10 { 11 12 get 13 { 14 if (_instance == null) 15 { 16 _instance = new UImanger(); 17 } 18 return _instance; 19 } 20 21 } 22 private UImanger() 23 { 24 25 JsonFromJx(); 26 27 } 28 29 public Dictionary<UIPanelType, string> UIPanelDic;//存取json資訊 30 //解析json資訊儲存到字典 31 public void JsonFromJx() 32 { 33 //載入文字 34 TextAsset text = Resources.Load<TextAsset>("UIPanelType");//載入json檔案 35 Debug.Log(text.text); 36 //解析json文字返回UIListFromJson物件 37 UIListFromJson listPanel = (UIListFromJson)JsonUtility.FromJson(text.text,typeof(UIListFromJson)); 38 Debug.Log(listPanel.ListInfo[0].Path); 39 UIPanelDic = new Dictionary<UIPanelType, string>(); 40 foreach (var item in listPanel.ListInfo) 41 { 42 // Debug.Log(listPanel); 43 UIPanelDic.Add(item.type, item.Path); 44 Debug.Log(item.Path); 45 } 46 } 47 public Dictionary<UIPanelType, BasePanel> Dic; 48 public BasePanel GetPanel(UIPanelType panelType) 49 { 50 51 if (Dic == null) 52 { 53 Dic = new Dictionary<UIPanelType, BasePanel>(); 54 } 55 BasePanel bp; 56 Dic.TryGetValue(panelType, out bp);//嘗試去獲取Key(panelType)對應的Panel 57 58 if (bp==null) 59 { 60 string path; 61 UIPanelDic.TryGetValue(panelType, out path); 62 GameObject obj = GameObject.Instantiate(Resources.Load(path)) as GameObject; 63 obj.transform.SetParent(GameObject.Find("Canvas").transform,false); 64 Dic = new Dictionary<UIPanelType, BasePanel>();//加載出來的UI都在存在Dic裡面 65 Dic.Add(panelType,obj.GetComponent<BasePanel>()); 66 return obj.GetComponent<BasePanel>(); 67 } 68 else 69 { 70 return bp; 71 72 } 73 74 75 } 76 77 public Stack<BasePanel> stack; 78 public void PushStack(UIPanelType uIPanel) 79 { 80 if (stack==null) 81 { 82 stack = new Stack<BasePanel>(); 83 } 84 if (stack.Count>0) 85 { 86 BasePanel panel= stack.Peek();//在入棧之前把棧裡面的第一個暫停(停止使用) 87 panel.OnPause(); 88 } 89 90 BasePanel bp= GetPanel(uIPanel);//開啟的PanelUI 91 bp.OnEnter(); 92 stack.Push(bp); 93 } 94 public void PopStack()//出棧最頂層,然後下面一層繼續可以操作 95 { 96 97 if (stack==null) 98 { 99 stack = new Stack<BasePanel>(); 100 } 101 if (stack.Count<=0) 102 { 103 return; 104 } 105 else 106 { 107 BasePanel bp= stack.Peek(); 108 bp.OnExit(); 109 stack.Pop(); 110 if (stack.Count<=0) 111 { 112 return; 113 } 114 BasePanel panel= stack.Peek(); 115 panel.OnContinue(); 116 } 117 118 119 120 } 121 122 }
第三個
1 齊培良老師 2018/10/15 15:45:18 2 using System.Collections; 3 using System.Collections.Generic; 4 using UnityEngine; 5 6 public class TaskPanel : BasePanel { 7 CanvasGroup ag; 8 // Use this for initialization 9 void Start () { 10 if (ag==null) 11 { 12 13 ag = GetComponent<CanvasGroup>(); 14 } 15 } 16 public override void OnEnter() 17 { 18 if (ag==null) 19 { 20 ag = GetComponent<CanvasGroup>(); 21 } 22 ag.alpha = 1; 23 ag.blocksRaycasts = true; 24 } 25 public override void OnExit() 26 { 27 ag.alpha = 0; 28 ag.blocksRaycasts = false; 29 } 30 public void OnClose() { 31 32 UImanger.Instance.PopStack(); 33 34 } 35 }
第四個:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class BasePanel : MonoBehaviour { 6 7 public virtual void OnEnter() { } 8 public virtual void OnPause() { } 9 public virtual void OnContinue() { } 10 public virtual void OnExit() { } 11 }
每一個預製體都有一個對應 的指令碼 沒有操作 多久害死空指令碼