Json.net无法序列化从Monobehaviour派生的类

时间:2019-03-08 14:35:15

标签: c# json unity3d json.net

我正在尝试序列化保存文件,但Json.net似乎无法序列化从Monobehaviour派生的类。它给我的错误如下

  

NotSupportedException:刚体属性已弃用

此项目中的任何地方都没有使用刚体,所以这个错误令我非常困惑。可能值得注意的是,我使用的是Json.net(3.5.8)的旧版本,因为这是支持项目框架(3.5)的最新版本

保存文件本身不是从Monobehaviour派生的,而是在其中的属性。这会导致错误出现。

这是我用来测试序列化的代码,如您所见,它应该序列化保存,打印出结果,对它进行脱序列化,然后再次打印出。

 var test1 = JsonConvert.SerializeObject(SaveManager.currentSave, Formatting.Indented, new JsonSerializerSettings()
            {
                PreserveReferencesHandling = PreserveReferencesHandling.Objects
            });

            Debug.Log(test1);

            var test2 = JsonConvert.DeserializeObject<Save>(test1);

            Debug.Log(test2);

保存课程

using System.Collections.Generic;
using UnityEngine;

namespace Com.BlewScreen.Woongame
{
    [System.Serializable]
    public class Save
    {
        private bool initialSave = true;

        private string saveName;

        //Player var
        private House house;
        private string districtHouseName;
        private House wantsToMove;
        private string districtWantsToMoveToName;

        [System.NonSerialized]
        public District houseDistrict;
        [System.NonSerialized]
        public District wantsToMoveToDistrict;

        private int income;
        private int health;
        private int balance;
        private int livingpleasure;
        private int regtime;
        private bool disabled;
        private bool hasPartner;
        private int kids;
        private int livingSituationCount;

        //Housing market var
        private DistrictStats a;    //Tom: Change this to "DistrictStatsA" etc. instead of just a, b, c, d, e
        private DistrictStats b;
        private DistrictStats c;
        private DistrictStats d;
        private DistrictStats e;

        private string characterIndexes;

        public string CharacterIndexes { get; set; }

        public string characterSkinTone;

        public string CharacterSkinTone { get; set; }

        public House House
        {
            get{ return house; }
            set{ house = value; }
        }
        public House WantsToMove
        {
            get{ return wantsToMove; }
            set{ wantsToMove = value; }
        }      
        public int Income
        {
            get { return income; }
            set{ income = value; }
        }
        public int Health
        {
            get{ return health; }
            set{ health = value; }
        }
        public int Balance
        {
            get{ return balance; }
            set{ balance = value;}
        }
        public int Livingpleasure
        {
            get{ return livingpleasure;}
            set{ livingpleasure = value;}
        }
        public int Regtime
        {
            get{ return regtime;}
            set{ regtime = value;}
        }
        public bool Disabled
        {
            get{ return disabled;}
            set{ disabled = value;}
        }
        public bool HasPartner
        {
            get{ return hasPartner;}
            set{ hasPartner = value;}
        }
        public int Kids
        {
            get{ return kids;}
            set{ kids = value;}
        }
        public DistrictStats A
        {
            get{ return a;}
            set{ a = value;}
        }
        public DistrictStats B
        {
            get{ return b; }
            set{ b = value;}
        }
        public DistrictStats C
        {
            get{ return c;}
            set { c = value;}
        }
        public DistrictStats D
        {
            get{ return d;}
            set{ d = value;}
        }
        public DistrictStats E
        {
            get{ return e;}
            set{ e = value; }
        }
        public string DistrictHouseName
        {
            get{ return districtHouseName;}
            set{ districtHouseName = value; }
        }
        public string DistrictWantsToMoveToName
        {
            get{ return districtWantsToMoveToName;}
            set{ districtWantsToMoveToName = value; }
        }
        public bool InitialSave
        {
            get{ return initialSave; }
            set{ initialSave = value;}
        }
        public int LivingSituationCount
        {
            get{ return livingSituationCount;}
            set{ livingSituationCount = value; }
        }
        public string SaveName
        {
            get{ return saveName; }
            set{ saveName = value; }
        }

        public Save()
        {
            InitialSave = true;
        }
        public Save(string saveName, House house, House wantsToMove, 
                    int income, int health, int balance, int livingpleasure, 
                    int regtime, bool disabled, bool hasPartner, int kids, 
                    int livingSituationCount, DistrictStats a, DistrictStats b, 
                    DistrictStats c, DistrictStats d, DistrictStats e, string characterIndexes, string characterSkinTone)
        {
            initialSave = false;
            DistrictHouseName = house.District.DistrictName;

            if(wantsToMove != null)
            {
                DistrictWantsToMoveToName = wantsToMove.District.DistrictName;

            }

            this.saveName = saveName;
            this.House = house;
            this.WantsToMove = wantsToMove;
            this.Income = income;
            this.Health = health;
            this.Balance = balance;
            this.Livingpleasure = livingpleasure;
            this.Regtime = regtime;
            this.Disabled = disabled;
            this.HasPartner = hasPartner;
            this.Kids = kids;
            this.LivingSituationCount = livingSituationCount;
            A = a;
            B = b;
            C = c;
            D = d;
            E = e;
            this.CharacterIndexes = characterIndexes;
            this.CharacterSkinTone = characterSkinTone;

        }
    }
}

完整错误堆栈

  

NotSupportedException:刚体属性已弃用   UnityEngine.GameObject.get_rigidbody()(在C:/buildslave/unity/build/Runtime/Export/GameObject.deprecated.cs:23)   (包装动态方法)UnityEngine.GameObject.Getrigidbody(对象)   Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(对象)   重新抛出为JsonSerializationException:从“ UnityEngine.GameObject”上的“刚体”获取值时出错。   Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(System.Object目标)   牛顿软件。 / p>

1 个答案:

答案 0 :(得分:1)

您无法序列化MonoBehaviour或任何Unity组件。

此错误很可能是Json.net试图获取.rigidbody属性值引起的,并且不建议使用它,将抛出该错误。

因此,如果避免该错误,该过程仍然会失败。从您的“保存”中删除任何Monobehaviours,或创建一个新类以包含要保存的数据。