ArgumentException: JSON must represent an object type

反序列化 枚举类型无法反序列化解决办法
看视频无意间发现个解决办法 记录下来

// ========================================================
// 描述:
// 作者:青龙 
// 创建时间: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();
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值