c# - Unity 5 游戏对象序列化

标签 c# json serialization unity-game-engine gameobject

我正在尝试使用 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 一起被弃用,请参阅 herehere

所有调用rigidbody的电话必须替换为 GetComponent<Rigidbody>()相反。

关于c# - Unity 5 游戏对象序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31786488/

相关文章:

c# - 使用 AddRange() 时如何增加 List<T> 的内部数组

java - 在这种情况下如何使用 Jackson Package?

javascript - 未捕获的语法错误 : Unexpected end of input While doing JSON. 解析

c# - 反射(reflect)类型时出现错误 - XML 序列化问题

c# - 三元表示法等同于 C# 中的 If Else 语句

c# - 尝试在任何 CPU 上使用 TaskScheduler 时出现 BadImageFormatException

Java JSONPARSER,验证JSON(解析之前或之后)

serialization - boost 序列化 : archive "unsupported version" exception

c++ - 使用 Qt 跨 HTTP Protocol Buffer 序列化中的空白

c# - 如何从以特定方式格式化的字符串之间访问特定值c#