反序列化 枚举类型无法反序列化解决办法
看视频无意间发现个解决办法 记录下来
// ========================================================
// 描述:
// 作者:青龙
// 创建时间:2020-04-19 12:08:26
// 版 本:1.0
// ========================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameRoot : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
UIManager.Instance.Test();
}
// Update is called once per frame
void Update()
{
}
}
// ========================================================
// 描述:
// 作者:青龙
// 创建时间:2020-04-19 11:58:15
// 版 本:1.0
// ========================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class UIManager
{
private UIManager() {
ParseUIPanelTypeJson();
}
private static UIManager instance;
public static UIManager Instance {
get{
if (instance == null)
{
instance = new UIManager();
}
return instance;
}
}
private Dictionary<UIPanelType, string> panelPathDict; //存储所有的面板prefab 的路径
[Serializable]
class UIPanelTyapJson {
public List<UIPanelInfo> infoList;
}
private void ParseUIPanelTypeJson() {
panelPathDict = new Dictionary<UIPanelType, string>();
TextAsset textAsset= Resources.Load<TextAsset>("UIPanelJson");
/// <summary>
/// 报错信息 ArgumentException: JSON must represent an object type.
///解决办法创建一个内部类UIPanelTyapJson 里面存储Dictionary
/// </summary>
//List <UIPanelInfo> uIPanelInfos= JsonUtility.FromJson<List<UIPanelInfo>>(textAsset.text);
// foreach (var info in uIPanelInfos)
// {
// panelPathDict.Add(info.panelType, info.path);
// }
/// <summary>
///解决办法创建一个内部类UIPanelTyapJson 里面存储Dictionary
/// </summary>
UIPanelTyapJson uIPanelTyapJson = JsonUtility.FromJson<UIPanelTyapJson>(textAsset.text);
foreach (var info in uIPanelTyapJson.infoList)
{
Debug.Log("panelType " + info.panelType);
Debug.Log("path " + info.path);
panelPathDict.Add(info.panelType, info.path);
}
}
public void Test() {
string path;
panelPathDict.TryGetValue(UIPanelType.Knapsack, out path);
Debug.Log(path);
}
}
{
"infoList":
[
{"panelTyeString":"ItemMasage",
"path":"UIpanel/ItemMassagePanel"
},
{"panelTyeString":"Knapsack",
"path":"UIpanel/KnapsackPanel"
},
{"panelTyeString":"MainMenu",
"path":"UIpanel/MainMenuPanel"
},
{"panelTyeString":"Shop",
"path":"UIpanel/ShopPanel"
},
{"panelTyeString":"Skill",
"path":"UIpanel/SkillPanel"
},
{"panelTyeString":"System",
"path":"UIpanel/SystemPanel"
},
{"panelTyeString":"Task",
"path":"UIpanel/TaskPanel"
}
]
}
// ========================================================
// 描述:
// 作者:青龙
// 创建时间:2020-04-19 11:28:41
// 版 本:1.0
// ========================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum UIPanelType {
ItemMasage,
Knapsack,
MainMenu,
Shop,
Skill,
System,
Task
}
// ========================================================
// 描述:
// 作者:青龙
// 创建时间:2020-04-19 11:51:34
// 版 本:1.0
// ========================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[Serializable]
public class UIPanelInfo :ISerializationCallbackReceiver
{
[NonSerialized]
public UIPanelType panelType;
public string panelTyeString;
// {
// get {
// return panelType.ToString();
// }
// set {
// UIPanelType type= (UIPanelType) Enum.Parse(typeof(UIPanelType), value);
// panelType = type;
// }
//}
public string path;
//序列化之后调用
public void OnAfterDeserialize()
{
// Debug.Log(panelTyeString);
UIPanelType type = (UIPanelType)Enum.Parse(typeof(UIPanelType), panelTyeString);
panelType = type;
}
//序列化之前调用
public void OnBeforeSerialize()
{
throw new NotImplementedException();
}
}