Unity 5 GameObject序列化 [英] Unity 5 GameObject serialization
问题描述
我正在尝试使用Newtonsoft.Json序列化整个GameObject. 当我使用JsonConvert序列化对象时,它将引发错误:
I am trying to serialize an entire GameObject with Newtonsoft.Json. When I serialize the object with JsonConvert it throws an error:
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的一部分
It's a part of DynamicValueProvider.cs
推荐答案
我不知道Newtonsoft.Json,但从错误消息来看-Unity 5中不赞成使用GameObject属性rigidbody
以及其他一些常见用法属性获取器,请参见此处,以及此处.
I don't know about Newtonsoft.Json, but judging from the error message - GameObject property rigidbody
has been deprecated in Unity 5 along with some other common property getters, see here and here.
所有对rigidbody
的调用都必须替换为GetComponent<Rigidbody>()
.
All calls to rigidbody
must be replaced with GetComponent<Rigidbody>()
instead.
这篇关于Unity 5 GameObject序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!