这里定义了一个枚举类型
public enum UIPanelType
{
Login,
Register,
ServerSelect,
StartMenu
}
这里定义了一个接收解析的json数据的类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class UIPanelInfo : ISerializationCallbackReceiver
{
[System.NonSerialized] public UIPanelType panelType; //json 无法解析枚举类型
public string panelTypeString;
public string path;
public void OnBeforeSerialize()
{
throw new System.NotImplementedException();
}
//每次反序列化之后执行
public void OnAfterDeserialize()
{
UIPanelType type = (UIPanelType)System.Enum.Parse(typeof(UIPanelType), panelTypeString);
panelType = type;
}
}
由上,不能解析的枚举类型在每次反序列化之后由解析的string类型赋值
文章展示了一个在Unity中使用C#处理JSON反序列化的例子。定义了一个枚举UIPanelType,然后创建了UIPanelInfo类,该类包含一个非序列化的枚举变量和对应的字符串变量。在反序列化后,通过Enum.Parse方法将字符串转换回枚举类型。
1019

被折叠的 条评论
为什么被折叠?



