我正在尝试序列化保存文件,但是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/runtime/export/gameobject.deprecated.cs:23) (包装器动态方法)UnityEngine.gameObject.getRigidBody(对象) newtonsoft.json.serialization.dynamicvalueprovider.getValue(object) 将Rethrow作为jsonserializationException:错误从" unityEngine.gameObject"上从"刚体"获取值。 newtonsoft.json.serialization.dynamicvalueprovider.getValue(system.Object target) /p>
您不能序列化单obehaviour或任何统一组件。
此错误很可能是由JSON.NET引起的,试图获得.rigidbody属性值,并且由于将其抛弃。
因此,如果避免了此错误,则该过程仍然会失败。从保存中删除所有单声道或创建新类以包含您要保存的数据。
有一些选择:
- 将
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
属性添加到组件类,然后将[JsonProperty]
属性添加到属性/字段。 - 为每个组件编写一个自定义的jsonconverter。
这些选项不适合我的用例,因为我有多个要序列化的组件和属性。我还想包括UnityEngine.Object
的name
属性,但我们不能将属性添加到类中。
我所做的是覆盖我的CreateProperties
方法。
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
var properties = base.CreateProperties(type, memberSerialization);
// Only filter class that is derived from MonoBehaviour
if (type.IsSubclassOf(typeof(MonoBehaviour)))
{
// Keep name property OR properties derived from MonoBehaviour
properties = properties.Where(x => x.PropertyName.Equals("name") || x.DeclaringType.IsSubclassOf(typeof(MonoBehaviour))).ToList();
}
return properties;
}