Я пытаюсь сериализовать файл сохранения, но Json.net не может сериализовать классы, производные от Monobehaviour. Ошибка, которую он мне дает, выглядит следующим образом
NotSupportedException: свойство solidbody устарело
В этом проекте нигде не используются твердые тела, поэтому эта ошибка меня очень смущает. Возможно, стоит отметить, что я использую старую версию 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: свойство solidbody устарело
UnityEngine.GameObject.get_rigidbody () (в C: /buildslave/unity/build/Runtime/Export/GameObject.deprecated.cs: 23)
(динамический метод-обертка) UnityEngine.GameObject.Getrigidbody (object)
Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue (объект)
Rethrow as JsonSerializationException: Ошибка при получении значения из «solidbody» в «UnityEngine.GameObject».
Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue (цель System.Object)
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter. 1024 *