序列化和Stack應用於UGUI(Unity 擴展)
- 2019 年 12 月 2 日
- 筆記
版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/CJB_King/article/details/78652777
1.擴展:
using UnityEngine; using System.Collections; using System.Collections.Generic; public static class DictionaryExtension { public static TValue TryGet<TKey,TValue>(this Dictionary<TKey,TValue> dict,TKey key ) { TValue value; dict.TryGetValue(key, out value); return value; } }
2:框架
using UnityEngine; using System.Collections; using System.Collections.Generic; using System; public class UIManager { private static UIManager _instance = null; public static UIManager Instance { get { if (_instance == null) { _instance = new UIManager(); } return _instance; } } private Transform canasTrans; public Transform canvasTransform { get { if (canasTrans == null) { canasTrans = GameObject.Find("Canvas").transform; } return canasTrans; } } private Dictionary<UIPanelType, string> panelDic = new Dictionary<UIPanelType, string>(); private Dictionary<UIPanelType, BasePanel> basePanelDic = new Dictionary<UIPanelType, BasePanel>(); private Stack<BasePanel> panelStack; private UIManager() { InitialUIPanel(); } public void InitialUIPanel() { TextAsset textAsset = Resources.Load<TextAsset>("UIPanelType"); UIPanelInfo UiPanelInfo = JsonUtility.FromJson<UIPanelInfo>(textAsset.text); foreach (UIPanel uipanel in UiPanelInfo.infoList) { if (!panelDic.ContainsKey(uipanel.uiPanelType)) { panelDic.Add(uipanel.uiPanelType, uipanel.path); } } } public void PushPanel(UIPanelType uiPanelType) { if (panelStack == null) panelStack = new Stack<BasePanel>(); if (panelStack.Count>0) { BasePanel firstPanel = panelStack.Peek(); firstPanel.OnPause(); } BasePanel secondPanel = TryGetBasePanel(uiPanelType); secondPanel.OnEnter(); if (!panelStack.Contains(secondPanel)) panelStack.Push(secondPanel); } public void PopPanel() { if (panelStack == null || panelStack.Count <= 0) return; if (panelStack.Count > 0) { BasePanel basePanel = panelStack.Pop(); basePanel.OnExit(); } if (panelStack.Count <= 0) return; BasePanel basePanel2 = panelStack.Peek(); basePanel2.OnResume(); } BasePanel TryGetBasePanel(UIPanelType uiPanelType) { BasePanel getBasePanel = basePanelDic.TryGet<UIPanelType, BasePanel>(uiPanelType); if (!getBasePanel) { string panelPath = panelDic.TryGet<UIPanelType, string>(uiPanelType); GameObject PanelGo = Resources.Load(panelPath) as GameObject; GameObject uiPanelGo = GameObject.Instantiate(PanelGo); uiPanelGo.transform.SetParent(canvasTransform, false); BasePanel basePanel = uiPanelGo.GetComponent<BasePanel>(); basePanelDic.Add(uiPanelType, basePanel); return basePanel; } else return getBasePanel; } } [Serializable] public class UIPanelInfo { public List<UIPanel> infoList; //infoList要與序列化文本中的json屬性名稱保持一致 } [Serializable] public class UIPanel : ISerializationCallbackReceiver { public string path; public string panelTypeString; [NonSerialized]//uiPanelType不會被序列化 public UIPanelType uiPanelType; public void OnBeforeSerialize() { } // 反序列化 從文本資訊 到對象 public void OnAfterDeserialize() { uiPanelType = (UIPanelType)Enum.Parse(typeof(UIPanelType), panelTypeString); } }
BasePanel:
using UnityEngine; using System.Collections; public class BasePanel : MonoBehaviour { public virtual void OnEnter() { Debug.Log(this.name); } public virtual void OnExit() { Debug.Log(this.name + "OnExit"); } public virtual void OnPause() { Debug.Log(this.name+"OnPause"); } public virtual void OnResume() { Debug.Log(this.name + "OnResume"); } }
Json文件對應:
{ "infoList": [ { "panelTypeString":"Message", "path":"UIPanel/MessagePanel" }, { "panelTypeString":"Start", "path":"UIPanel/StartPanel" }, { "panelTypeString":"Login", "path":"UIPanel/LoginPanel" }, { "panelTypeString":"Register", "path":"UIPanel/RegisterPanel" } ] }