public static SerializerFactory DefaultFactory() { var serializer = new SerializerFactory(); serializer.RegisterSerializer(new MaterialSerializer()); serializer.RegisterSerializer(new SkinnedMeshSerializer()); return serializer; }
protected override void Serialize(Material obj, ref BinaryWriter writer, SerializerFactory serializerFactory) { writer.Write(obj.shader.name); writer.Write(Colors.Length); writer.Write(Textures.Length); writer.Write(Floats.Length); writer.Write(Matrices.Length); foreach (var c in Colors) { //we write the property names so others can deserialize writer.Write(c); writer.Write(obj.GetColor(c)); } foreach (var t in Textures) { writer.Write(t); var tex = obj.GetTexture(t); if (tex) { //todo: write a better texture path writer.Write(tex.name); } else { writer.Write(""); } } foreach (var f in Floats) { //todo: write float values } foreach (var m in Matrices) { //todo: write matrix values } }
/// <summary> /// set up with a custom factory /// </summary> /// <param name="serializer"></param> public UnityModelFile(SerializerFactory serializer) { _serializer = serializer; }
/// <summary> /// set up with a DefaultFactory /// </summary> public UnityModelFile() { _serializer = SerializerFactory.DefaultFactory(); }
protected override void Serialize(Component obj, ref BinaryWriter writer, SerializerFactory serializer) { throw new NotImplementedException(); }
protected override Component Deserialize(ref BinaryReader reader, Object parent, SerializerFactory serializer) { throw new NotImplementedException(); }
protected abstract void Serialize(T obj, ref BinaryWriter writer, SerializerFactory serializer);
protected abstract T Deserialize(ref BinaryReader reader, Object parent, SerializerFactory serializer);
Object IObjectSerializer.Deserialize(ref BinaryReader reader, Object parent, SerializerFactory serializerFactory) { return(Deserialize(ref reader, parent, serializerFactory)); }
void IObjectSerializer.Serialize(Object obj, ref BinaryWriter writer, SerializerFactory serializerFactory) { Serialize(obj as T, ref writer, serializerFactory); }
protected override Material Deserialize(ref BinaryReader reader, Object parent, SerializerFactory serializerFactory) { var shaderName = reader.ReadString(); var shader = Shader.Find(shaderName); var mat = new Material(shader); var colorCount = reader.ReadInt32(); var textureCount = reader.ReadInt32(); var floatCount = reader.ReadInt32(); var matrixCount = reader.ReadInt32(); string prop; for (int i = 0; i < colorCount; i++) { prop = reader.ReadString(); mat.SetColor(prop, reader.ReadColor()); } for (int i = 0; i < textureCount; i++) { prop = reader.ReadString(); var texPath = reader.ReadString(); //TODO: load texture from path } for (int i = 0; i < floatCount; i++) { prop = reader.ReadString(); } for (int i = 0; i < matrixCount; i++) { prop = reader.ReadString(); } return(mat); }
protected override void Serialize(SkinnedMeshRenderer obj, ref BinaryWriter writer, SerializerFactory serializer) { Stream stream = null; if (StoreInline) { stream = writer.BaseStream; } else { //TODO: write relative file path throw new NotImplementedException("Cannot store meshes in a separate file yet"); } //store bones writer.Write(obj.bones.Length); foreach (var bone in obj.bones) { writer.Write(serializer.ReferenceMap[bone]); } writer.Write(obj.sharedMaterials.Length); foreach (var material in obj.sharedMaterials) { serializer.InternalSerialize(material, ref writer); } writer.Write((int)obj.quality); writer.Write(obj.localBounds); //store mesh UnityMeshFile.Write(stream, obj.sharedMesh, meshConfig); }
protected override SkinnedMeshRenderer Deserialize(ref BinaryReader reader, Object parent, SerializerFactory serializer) { Stream stream = null; if (StoreInline) { stream = reader.BaseStream; } else { //TODO: write relative file path throw new NotImplementedException("Cannot read meshes from a separate file yet"); } var skin = (parent as Transform).gameObject.AddComponent <SkinnedMeshRenderer>(); var boneCount = reader.ReadInt32(); var bones = new Transform[boneCount]; for (int i = 0; i < bones.Length; i++) { var boneID = reader.ReadInt32(); bones[i] = serializer.ReferenceMap[boneID] as Transform; } var materialCount = reader.ReadInt32(); var materials = new Material[materialCount]; //todo: serialize materials for (int i = 0; i < materialCount; i++) { materials[i] = serializer.InternalDeserialize(ref reader, skin) as Material; } var quality = (SkinQuality)reader.ReadInt32(); var bounds = reader.ReadBounds(); var mesh = UnityMeshFile.Read(stream, meshConfig); skin.sharedMesh = mesh; skin.bones = bones; skin.quality = quality; skin.localBounds = bounds; return(skin); }