版权声明:本文为博主原创文章,遵循 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"
}
]
}