在Unity 3D中,有一个带有Transform属性的GameObject。变换具有位置(Vector3)和旋转(四元数)。我想通过protobuf-net将这些GameObjects发送到后备存储中。我目前正在使用以下代码,但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);
}
我似乎回到了正确数量的GameObjects,但反序列化唯一正确的是.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编辑器中执行。