我正在尝试用Newtonsoft.Json序列化整个GameObject。当我用JsonConvert序列化对象时,它抛出了一个错误:
NotSupportedException: rigidbody property has been deprecated
UnityEngine.GameObject.get_rigidbody () (at C:/buildslave/unity/build/Runtime/Export/UnityEngineGameObject_Deprecated.cs:23)
(wrapper dynamic-method) UnityEngine.GameObject.Getrigidbody (object) <IL 0x00006, 0x00073>
Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue (object) (at Assets/JsonDotNet/Source/Serialization/DynamicValueProvider.cs:104)
Rethrow as JsonSerializationException: Error getting value from 'rigidbody' on 'UnityEngine.GameObject'.
Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue (System.Object target) (at Assets/JsonDotNet/Source/Serialization/DynamicValueProvider.cs:108)
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContract collectionValueContract) (at Assets/JsonDotNet/Source/Serialization/JsonSerializerInternalWriter.cs:338)为什么会发生这种情况?
这里有个问题:
public object GetValue(object target)
{
try
{
if (_getter == null)
_getter = DynamicReflectionDelegateFactory.Instance.CreateGet<object>(_memberInfo);
return _getter(target);
}
catch (Exception ex)
{
throw new JsonSerializationException("Error getting value from '{0}' on '{1}'.".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), ex);
}
}它是DynamicValueProvider.cs的一部分
发布于 2015-08-03 21:24:49
我不知道Newtonsoft.Json,但从错误消息判断- GameObject属性rigidbody以及其他一些常见的属性getter在Unity5中已被弃用,请参阅here和here。
所有对rigidbody的调用都必须替换为GetComponent<Rigidbody>()。
发布于 2020-09-25 17:14:24
我解决这个问题的方法是让Newtonsoft只序列化我感兴趣的字段。要将您的属性‘白名单’序列化,您可以将JsonObject(MemberSerialization.OptIn)标记添加到您的类,并将JsonProperty添加到您感兴趣的属性中。
不过,如果你的意图是在Monobehaviour中序列化Unity的属性,那么显然这对你是行不通的。
发布于 2019-05-23 13:10:41
错误消息是虚假的。这是因为JsonConvert通过遍历所有属性来执行序列化的方式。哪一个先失败或多或少是随机的。
真正的问题是,从MonoBehaviour派生的类不能简单地序列化。您需要找出一种方法,只序列化您感兴趣的数据值,而不是整个类。
搜索“serialise MonoBehaviour”以获得一些如何做到这一点的想法。推荐的方法是使用ScriptableObject。
https://stackoverflow.com/questions/31786488
复制相似问题