UIPanelType.json
{
"infoList":
[
{"panelTypeString":"ItemMessage",
"path":"UIPanel/ItemMessagePanel"},
{"panelTypeString":"Knapsack",
"path":"UIPanel/KnapsackPanel"},
{"panelTypeString":"MainMenu",
"path":"UIPanel/MainMenuPanel"},
{"panelTypeString":"Shop",
"path":"UIPanel/ShopPanel"},
{"panelTypeString":"Skill",
"path":"UIPanel/SkillPanel"},
{"panelTypeString":"System",
"path":"UIPanel/SystemPanel"},
{"panelTypeString":"Task",
"path":"UIPanel/TaskPanel"}
]
}
UIPanelInfo
using UnityEngine;
using System.Collections;
using System;
[Serializable]
public class UIPanelInfo :ISerializationCallbackReceiver {
[NonSerialized]
public UIPanelType panelType;
public string panelTypeString;
//{
// get
// {
// return panelType.ToString();
// }
// set
// {
// UIPanelType type =(UIPanelType)System.Enum.Parse(typeof(UIPanelType), value);
// panelType = type;
// }
//}
public string path;
// 反序列化 从文本信息 到对象
public void OnAfterDeserialize()
{
Debug.Log("开始调用");
UIPanelType type = (UIPanelType)System.Enum.Parse(typeof(UIPanelType), panelTypeString);
panelType = type;
}
public void OnBeforeSerialize()
{
}
}
UIManage脚本
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine.UI;
public class UIManager
{
///
/// 单例模式的核心
/// 1,定义一个静态的对象 在外界访问 在内部构造
/// 2,构造方法私有化
private static UIManager _instance;
public static UIManager Instance
{
get
{
if (_instance == null)
{
_instance = new UIManager();
}
return _instance;
}
}
private Transform canvasTransform;
public Transform CanvasTransform
{
get {
if (canvasTransform == null)
{
canvasTransform = GameObject.Find("Canvas").transform;
}
return canvasTransform;
}
set {; }
}
private Dictionary<UIPanelType, string> panelPathDict;//存储所有面板Prefab的路径
private Dictionary<UIPanelType, BasePanel> panelDict;//保存所有实例化面板的游戏物体身上的BasePanel组件
private Stack<BasePanel> panelStack;
private UIManager()
{
ParseUIPanelTypeJson();
}
/// <summary>
///入栈
/// </summary>
public void PushPanel(UIPanelType panelType)
{
if (panelStack == null)
{
panelStack = new Stack<BasePanel>();
}
if (panelStack.Count > 0)
{
BasePanel topPanel = panelStack.Peek();
topPanel.OnPause();
}
BasePanel panel = GetPanel(panelType);
panel.OnEnter();
panelStack.Push(panel);
}
/// <summary>
/// 出栈
/// </summary>
public void PopPanel()
{
if (panelStack == null)
{
panelStack = new Stack<BasePanel>();
}
Debug.Log(panelStack.Count);
if (panelStack.Count <= 0)
{
Debug.Log("asdfasdfasd");
return;
}
BasePanel topPanel = panelStack.Peek();
topPanel.OnExit();
panelStack.Pop();
Debug.Log(panelStack.Count);
if (panelStack.Count <= 0)
{
return;
}
BasePanel topPanel2 = panelStack.Peek();
topPanel2.OnResume();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private BasePanel GetPanel(UIPanelType panelType)
{
if (panelDict == null)
{
panelDict = new Dictionary<UIPanelType, BasePanel>();
}
//BasePanel panel;
//panelDict.TryGetValue(panelType, out panel);//TODO
BasePanel panel = panelDict.TryGet(panelType);
if (panel == null)
{
//如果找不到,那么就找这个面板的Prefab的路径,然后去根据prefab 去实例化面板
//string path;
//panelPathDict.TryGetValue(panelType, out path);
string path = panelPathDict.TryGet(panelType);
GameObject instPanel = GameObject.Instantiate(Resources.Load(path)) as GameObject;
instPanel.transform.SetParent(CanvasTransform,false);//TODO
panelDict.Add(panelType, instPanel.GetComponent<BasePanel>());
return instPanel.GetComponent<BasePanel>();
}
else
{
return panel;
}
}
[Serializable]
class UIPanelTypeJson
{
public List<UIPanelInfo> infoList;
}
private void ParseUIPanelTypeJson()
{
panelPathDict = new Dictionary<UIPanelType, string>();
TextAsset ta = Resources.Load<TextAsset>("UIPanelType");
//因为这种方法反序列化不了带枚举类型的对象 所以
UIPanelTypeJson jsonObject = JsonUtility.FromJson<UIPanelTypeJson>(ta.text);
foreach (UIPanelInfo info in jsonObject.infoList)
{
panelPathDict.Add(info.panelType, info.path);
}
}
/// <summary>
/// just for test
/// </summary>
public void Test()
{
string path ;
panelPathDict.TryGetValue(UIPanelType.Knapsack,out path);
}
}