错误:
Self referencing loop detected for property 'normalized' with type 'UnityEngine.Vector3'. Path '[0].coor'.
错误原因:
NewtonSoft has a hard time serializing structs (Vector3 is defined as a struct). More specifically that struct has a property normalized that actually returns a Vector3 also, that Vector3 also has a normalized property and so on, so what happens is NewtonSoft is trying to serialize that new Vector3 everytime and you end up in an infinite loop.
解决方案1:
My workaround for this was to create a custom class for this purpose:
[System.Serializable]
public class SerializableVector3{
public float x;
public float y;
public float z;
[JsonIgnore]
public Vector3 UnityVector{
get{
return new Vector3(x, y, z);
}
}
public SerializableVector3(Vector3 v){
x = v.x;
y = v.y;
z = v.z;
}
public static List<SerializableVector3> GetSerializableList(List<Vector3> vList){
List<SerializableVector3> list = new List<SerializableVector3>(vList.Count);
for(int i = 0 ; i < vList.Count ; i++){
list.Add(new SerializableVector3(vList[i]));
}
return list;
}
public static List<Vector3> GetSerializableList(List<SerializableVector3> vList){
List<Vector3> list = new List<Vector3>(vList.Count);
for(int i = 0 ; i < vList.Count ; i++){
list.Add(vList[i].UnityVector);
}
return list;
}
}
Whenever you serialize a class make sure to convert all your Vector3 to SerializableVector3 and vice-versa when you deserialize. I use the ISerializable interface (System.Runtime.Serialization) for this purpose among other things.
JsonSerializationException: Self referencing loop detected - Unity Forum
解决方案2: