//==========================
// - FileName: JsonManager.cs
// - Created: true.
// - CreateTime: 2020/06/26 00:37:09
// - Email: 1670328571@qq.com
// - Region: China WUHAN
// - Description: Json 读取
//==========================
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JsonUtilityManager:BaseManager
{
/构造函数
public JsonUtilityManager(GameManager gameManager) : base(gameManager)
{
//ParseUIPanelTypeJson();
}
private Dictionary<UIPanelType, string> panelPathDict;//存储所有面板Prefab的路径
///序列化json对象(必须有)
[Serializable]
class UIPanelTypeJson
{
public List<UIPanelInfo> infoList;//这个名字要和json数据中的对象对应
}
///读取json对象的内容
private void ParseUIPanelTypeJson()
{
panelPathDict = new Dictionary<UIPanelType, string>();
TextAsset ta = Resources.Load<TextAsset>("Json/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);
panelPathDict.TryGetValue(UIPanelType.ItemMessage, out path);
Debug.Log(path);
panelPathDict.TryGetValue(UIPanelType.MainMenu, out path);
Debug.Log(path);
panelPathDict.TryGetValue(UIPanelType.Shop, out path);
Debug.Log(path);
panelPathDict.TryGetValue(UIPanelType.Skill, out path);
Debug.Log(path);
panelPathDict.TryGetValue(UIPanelType.System, out path);
Debug.Log(path);
panelPathDict.TryGetValue(UIPanelType.Task, out path);
Debug.Log(path);
}
}
Manager Of Managers(九)JsonManager
最新推荐文章于 2024-01-14 15:01:37 发布
本文介绍了一个Unity项目中使用的JsonManager类,该类负责从Json文件中解析UI面板类型及其对应的Prefab路径。通过加载Json/UIPanelType资源并使用JsonUtility.FromJson方法,将文本资产转换为UIPanelTypeJson对象,然后遍历对象的infoList属性,将面板类型和路径存储到Dictionary中。
摘要由CSDN通过智能技术生成