Protobuf-net [反]序列化 GameObject (Unity 3D)
在Unity 3D中,有一个带有Transform属性的GameObject。变换具有位置(Vector3)和旋转(四元数)。我想通过 protobuf-net 将这些游戏对象发送到后备存储或从后备存储发送这些游戏对象。我目前正在使用以下代码执行此操作,但 GameObject.transform.position.x,y,z 和 transform.rotation.x,y,z,w 似乎没有存储在序列化文件中?
RuntimeTypeModel model = TypeModel.Create();
model.AutoAddMissingTypes = true;
model.Add(typeof(Vector3), true).Add("x","y","z");
model.Add(typeof(Transform), true).Add("position").Add("rotation");
model.Add(typeof(Quaternion), true).Add("x","y","z","w");
model.Add(typeof(GameObject), true).Add("transform").Add("name");
model.SerializeWithLengthPrefix(fs, go, typeof(GameObject), PrefixStyle.Base128, 0);
反序列化:
using (FileStream fs = new FileStream(path, FileMode.Open))
{
fs.Position = 0;
RuntimeTypeModel model = TypeModel.Create();
model.AutoAddMissingTypes = true;
model.Add(typeof(Vector3), true).Add("x","y","z");
model.Add(typeof(Transform), true).Add("position").Add("rotation"); ;
model.Add(typeof(Quaternion), true).Add("x","y","z","w");
model.Add(typeof(GameObject), true).Add("transform").Add("name");
do
{
len = ProtoReader.ReadLengthPrefix(fs, false, PrefixStyle.Base128, out fieldNumber, out bytesRead);
if (bytesRead <= 0) continue;
gos.Add((GameObject)model.Deserialize(fs, null, typeof(GameObject), len));
} while (bytesRead > 0);
}
我似乎得到了正确数量的游戏对象,但反序列化时唯一正确的是 .name 属性。我当前的代码未转换 Transform 类的子属性。任何想法都会非常有帮助!
谢谢
-编辑
根据我的评论,这里是堆栈跟踪:
at (wrapper managed-to-native) UnityEngine.Object.set_name (string) <0x00004>
at (wrapper dynamic-method) UnityEngine.Transform.proto_22 (object,ProtoBuf.ProtoReader) <IL 0x0002f, 0x00137>
at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read (object,ProtoBuf.ProtoReader) <IL 0x00008, 0x0002f>
at ProtoBuf.Meta.RuntimeTypeModel.Deserialize (int,object,ProtoBuf.ProtoReader) <IL 0x0003d, 0x00143>
at ProtoBuf.ProtoReader.ReadTypedObject (object,int,ProtoBuf.ProtoReader,System.Type) <IL 0x0002d, 0x000f6>
at ProtoBuf.ProtoReader.ReadObject (object,int,ProtoBuf.ProtoReader) <IL 0x00004, 0x00031>
at (wrapper dynamic-method) CoreStructure.proto_20 (object,ProtoBuf.ProtoReader) <IL 0x000e5, 0x005cb>
at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read (object,ProtoBuf.ProtoReader) <IL 0x00008, 0x0002f>
at ProtoBuf.Meta.RuntimeTypeModel.Deserialize (int,object,ProtoBuf.ProtoReader) <IL 0x0003d, 0x00143>
at ProtoBuf.ProtoReader.ReadTypedObject (object,int,ProtoBuf.ProtoReader,System.Type) <IL 0x0002d, 0x000f6>
at ProtoBuf.ProtoReader.ReadObject (object,int,ProtoBuf.ProtoReader) <IL 0x00004, 0x00031>
at (wrapper dynamic-method) CharacterPoseSet.proto_14 (object,ProtoBuf.ProtoReader) <IL 0x00116, 0x006e0>
at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read (object,ProtoBuf.ProtoReader) <IL 0x00008, 0x0002f>
at ProtoBuf.Meta.RuntimeTypeModel.Deserialize (int,object,ProtoBuf.ProtoReader) <IL 0x0003d, 0x00143>
at ProtoBuf.Meta.TypeModel.DeserializeCore (ProtoBuf.ProtoReader,System.Type,object,bool) <IL 0x00011, 0x00073>
at ProtoBuf.Meta.TypeModel.Deserialize (System.IO.Stream,object,System.Type,ProtoBuf.SerializationContext) <IL 0x00022, 0x000ff>
at ProtoBuf.Meta.TypeModel.Deserialize (System.IO.Stream,object,System.Type) <IL 0x00005, 0x0003e>
at PoseEditor.DeserializeCharacterPoseSet () [0x0015f] in C:\My Work\KungFuKitty\UnityWorkspace\Assets\Scripts\Editor\PoseBuilder\PoseEditor.cs:137
UnityEngine.Debug:LogError(Object)
PoseEditor:DeserializeCharacterPoseSet() (at Assets/Scripts/Editor/PoseBuilder/PoseEditor.cs:160)
PoseEditor:OnGUI() (at Assets/Scripts/Editor/PoseBuilder/PoseEditor.cs:98)
PoseBuilder:OnGUI() (at Assets/Scripts/Editor/PoseBuilder/PoseBuilder.cs:128)
UnityEditor.DockArea:OnGUI()
反序列化:
RuntimeTypeModel model = TypeModel.Create();
model.AutoAddMissingTypes = false;
model.Add(typeof(CharacterPoseSet), true);
model.Add(typeof(LegStructure), true);
model.Add(typeof(ArmStructure), true);
model.Add(typeof(AuxilaryStructure), true);
model.Add(typeof(CoreStructure), true);
model.Add(typeof(Vector3), true).Add("x", "y", "z"); <-- UnityEngine
model.Add(typeof(Transform), true).Add("name"); <-- UnityEngine
model.Add(typeof(Quaternion), true).Add("x", "y", "z", "w"); <-- UnityEngine
model.Add(typeof(GameObject), true).Add("transform").Add("name").Add("layer"); <--..
cps = model.Deserialize(fs, cps, typeof(CharacterPoseSet)) as CharacterPoseSet;
这是在 PC/Windows 7 Box 上..所有代码都在 Unity 编辑器内执行。
In Unity 3D, there is a GameObject with a Transform property. Transform has position (Vector3) and rotation (Quaternion). I would like to send these GameObjects to/from a backing store via protobuf-net. I am currently doing so with the following code but the GameObject.transform.position.x,y,z and transform.rotation.x,y,z,w don't appear to be stored in the serialized file?
RuntimeTypeModel model = TypeModel.Create();
model.AutoAddMissingTypes = true;
model.Add(typeof(Vector3), true).Add("x","y","z");
model.Add(typeof(Transform), true).Add("position").Add("rotation");
model.Add(typeof(Quaternion), true).Add("x","y","z","w");
model.Add(typeof(GameObject), true).Add("transform").Add("name");
model.SerializeWithLengthPrefix(fs, go, typeof(GameObject), PrefixStyle.Base128, 0);
Deserialize:
using (FileStream fs = new FileStream(path, FileMode.Open))
{
fs.Position = 0;
RuntimeTypeModel model = TypeModel.Create();
model.AutoAddMissingTypes = true;
model.Add(typeof(Vector3), true).Add("x","y","z");
model.Add(typeof(Transform), true).Add("position").Add("rotation"); ;
model.Add(typeof(Quaternion), true).Add("x","y","z","w");
model.Add(typeof(GameObject), true).Add("transform").Add("name");
do
{
len = ProtoReader.ReadLengthPrefix(fs, false, PrefixStyle.Base128, out fieldNumber, out bytesRead);
if (bytesRead <= 0) continue;
gos.Add((GameObject)model.Deserialize(fs, null, typeof(GameObject), len));
} while (bytesRead > 0);
}
I appear to be getting back the correct number of GameObjects but the only thing that is correct on deserialization is the .name property. The sub-properties of the Transform class aren't being translated with my current code. Any ideas would be super helpful!
Thanks-
EDIT
Based on my comment, here is the stack trace:
at (wrapper managed-to-native) UnityEngine.Object.set_name (string) <0x00004>
at (wrapper dynamic-method) UnityEngine.Transform.proto_22 (object,ProtoBuf.ProtoReader) <IL 0x0002f, 0x00137>
at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read (object,ProtoBuf.ProtoReader) <IL 0x00008, 0x0002f>
at ProtoBuf.Meta.RuntimeTypeModel.Deserialize (int,object,ProtoBuf.ProtoReader) <IL 0x0003d, 0x00143>
at ProtoBuf.ProtoReader.ReadTypedObject (object,int,ProtoBuf.ProtoReader,System.Type) <IL 0x0002d, 0x000f6>
at ProtoBuf.ProtoReader.ReadObject (object,int,ProtoBuf.ProtoReader) <IL 0x00004, 0x00031>
at (wrapper dynamic-method) CoreStructure.proto_20 (object,ProtoBuf.ProtoReader) <IL 0x000e5, 0x005cb>
at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read (object,ProtoBuf.ProtoReader) <IL 0x00008, 0x0002f>
at ProtoBuf.Meta.RuntimeTypeModel.Deserialize (int,object,ProtoBuf.ProtoReader) <IL 0x0003d, 0x00143>
at ProtoBuf.ProtoReader.ReadTypedObject (object,int,ProtoBuf.ProtoReader,System.Type) <IL 0x0002d, 0x000f6>
at ProtoBuf.ProtoReader.ReadObject (object,int,ProtoBuf.ProtoReader) <IL 0x00004, 0x00031>
at (wrapper dynamic-method) CharacterPoseSet.proto_14 (object,ProtoBuf.ProtoReader) <IL 0x00116, 0x006e0>
at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read (object,ProtoBuf.ProtoReader) <IL 0x00008, 0x0002f>
at ProtoBuf.Meta.RuntimeTypeModel.Deserialize (int,object,ProtoBuf.ProtoReader) <IL 0x0003d, 0x00143>
at ProtoBuf.Meta.TypeModel.DeserializeCore (ProtoBuf.ProtoReader,System.Type,object,bool) <IL 0x00011, 0x00073>
at ProtoBuf.Meta.TypeModel.Deserialize (System.IO.Stream,object,System.Type,ProtoBuf.SerializationContext) <IL 0x00022, 0x000ff>
at ProtoBuf.Meta.TypeModel.Deserialize (System.IO.Stream,object,System.Type) <IL 0x00005, 0x0003e>
at PoseEditor.DeserializeCharacterPoseSet () [0x0015f] in C:\My Work\KungFuKitty\UnityWorkspace\Assets\Scripts\Editor\PoseBuilder\PoseEditor.cs:137
UnityEngine.Debug:LogError(Object)
PoseEditor:DeserializeCharacterPoseSet() (at Assets/Scripts/Editor/PoseBuilder/PoseEditor.cs:160)
PoseEditor:OnGUI() (at Assets/Scripts/Editor/PoseBuilder/PoseEditor.cs:98)
PoseBuilder:OnGUI() (at Assets/Scripts/Editor/PoseBuilder/PoseBuilder.cs:128)
UnityEditor.DockArea:OnGUI()
Deserialization:
RuntimeTypeModel model = TypeModel.Create();
model.AutoAddMissingTypes = false;
model.Add(typeof(CharacterPoseSet), true);
model.Add(typeof(LegStructure), true);
model.Add(typeof(ArmStructure), true);
model.Add(typeof(AuxilaryStructure), true);
model.Add(typeof(CoreStructure), true);
model.Add(typeof(Vector3), true).Add("x", "y", "z"); <-- UnityEngine
model.Add(typeof(Transform), true).Add("name"); <-- UnityEngine
model.Add(typeof(Quaternion), true).Add("x", "y", "z", "w"); <-- UnityEngine
model.Add(typeof(GameObject), true).Add("transform").Add("name").Add("layer"); <--..
cps = model.Deserialize(fs, cps, typeof(CharacterPoseSet)) as CharacterPoseSet;
This is on PC/Windows 7 Box.. all code is executing inside the Unity editor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论