错误:
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:

ReferenceLoopHandling setting

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐