我正在尝试使用 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 的一部分
最佳答案
我不了解Newtonsoft.Json,但从错误消息来看 - GameObject property rigidbody
已在 Unity 5 中与其他一些常见属性 getter 一起被弃用,请参阅 here和 here 。
所有调用rigidbody
的电话必须替换为 GetComponent<Rigidbody>()
相反。
关于c# - Unity 5 游戏对象序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31786488/