Unity 中 JsonUtility的使用
写一个Json文件 格式如下 必须是一个{} 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"} ] }
using System; 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; private Transform CanvasTransform { get { if (canvasTransform == null) { canvasTransform = GameObject.Find("Canvas").transform; } return canvasTransform; } } 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>(); if (panelStack.Count <= 0) return; //关闭栈顶页面的显示 BasePanel topPanel = panelStack.Pop(); topPanel.OnExit(); 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); 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) { //Debug.Log(info.panelType); panelPathDict.Add(info.panelType, info.path); } } /// <summary> /// just for test /// </summary> public void Test() { string path ; panelPathDict.TryGetValue(UIPanelType.Knapsack,out path); Debug.Log(path); } }
下面是 UIPanelInfo.cs
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() { UIPanelType type = (UIPanelType)System.Enum.Parse(typeof(UIPanelType), panelTypeString); panelType = type; } public void OnBeforeSerialize() { } }
以下是 UIPanelType.cs
using UnityEngine; using System.Collections; using System; public enum UIPanelType { ItemMessage, Knapsack, MainMenu, Shop, Skill, System, Task }
这句代码用来读取 Resources目录下名为UIPanelType名字的 文件 返回一个 ta对象
TextAsset ta = Resources.Load<TextAsset>("UIPanelType");
下面代码用来解析ta.text 为 UIPanelTypeJson的对象
UIPanelTypeJson jsonObject = JsonUtility.FromJson<UIPanelTypeJson>(ta.text);
注意 UIPanelTypeJson类 要标识为 可序列号 如下:
[Serializable] class UIPanelTypeJson { public List<UIPanelInfo> infoList; }
UIPanelInfo 类 需要实现 ISerializationCallbackReceiver 这个接口 用来响应 序列化过程中的两个方法 OnAfterDeserialize (序列化结束)和 OnBeforeSerialize(序列化前)
以下代码 将 string类型 转换成 enum类型
UIPanelType type = (UIPanelType)System.Enum.Parse(typeof(UIPanelType), panelTypeString);
相关教程 请参阅 UI框架 - 基于Unity5.3UGUI 课时11-12的讲解
浙公网安备 33010602011771号