Unity 3D has a GameObject with the Transform property. Transformation has position (Vector3) and rotation (Quaternion). I would like to send these GameObjects to / from the backup storage via protobuf-net. I am currently doing this with the following code, but the GameObject.transform.position.x, y, z and transform.rotation.x, y, z, w files do not seem 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 seem to be returning the correct number of GameObjects, but the only thing that is true when deserializing is the .name property. Sub-properties of the Transform class are not translated with my current code. Any ideas would be very 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 a PC / Windows 7 Box .. all code is executed inside the Unity editor.