private PrefabEntity RecursivelyReadPrefabEntity(BinaryReader reader) { string name = reader.ReadString(); PrefabEntity entity = new PrefabEntity(name); entity.Position = ReadStruct <Vector3>(reader); entity.Scale = ReadStruct <Vector3>(reader); entity.Rotation = ReadStruct <Quaternion>(reader); entity.OriginOffset = ReadStruct <Vector3>(reader); string meshID = reader.ReadString(); entity.Mesh = !string.IsNullOrEmpty(meshID) ? (Mesh)AssetManager.Assets[meshID] : null; string materialID = reader.ReadString(); entity.Material = !string.IsNullOrEmpty(materialID) ? (Material)AssetManager.Assets[materialID] : null; int childrenCount = reader.ReadInt32(); for (int i = 0; i < childrenCount; i++) { entity.Children.Add(RecursivelyReadPrefabEntity(reader)); } return(entity); }
public Prefab(string name, PrefabEntity rootEntity, string id = null) { Name = name; RootPrefabEntity = rootEntity; ID = id != null ? id : RNG.GetRandomID(); DataManager.Prefabs.Add(ID, this); }
public static Prefab Deserialize(BinaryReader reader) { //read name string name = reader.ReadString(); //read id string id = reader.ReadString(); //read entities PrefabEntity rootEntity = RecursivelyDeserialize(reader); return(new Prefab(name, rootEntity, id)); }
private void RecursivelyWritePrefabEntity(BinaryWriter writer, PrefabEntity entity) { writer.Write(entity.Name); WriteStruct(writer, entity.Position); WriteStruct(writer, entity.Scale); WriteStruct(writer, entity.Rotation); WriteStruct(writer, entity.OriginOffset); writer.Write(entity.Mesh != null ? entity.Mesh.ID : string.Empty); writer.Write(entity.Material != null ? entity.Material.ID : string.Empty); writer.Write(entity.Children.Count); foreach (PrefabEntity child in entity.Children) { RecursivelyWritePrefabEntity(writer, child); } }
private void RecursivelySerialize(PrefabEntity entity, BinaryWriter writer) { //write name writer.Write(entity.Name); //write transform writer.Write(entity.Position.X); writer.Write(entity.Position.Y); writer.Write(entity.Position.Z); //position writer.Write(entity.Rotation.X); writer.Write(entity.Rotation.Y); writer.Write(entity.Rotation.Z); writer.Write(entity.Rotation.W); //rotation writer.Write(entity.Scale.X); writer.Write(entity.Scale.Y); writer.Write(entity.Scale.Z); //scale writer.Write(entity.OriginOffset.X); writer.Write(entity.OriginOffset.Y); writer.Write(entity.OriginOffset.Z); //origin //write mesh & material writer.Write(entity.Mesh != null); if (entity.Mesh != null) { writer.Write(entity.Mesh.ID); } writer.Write(entity.Material != null); if (entity.Material != null) { writer.Write(entity.Material.ID); } //write children writer.Write(entity.Children.Count); //children count foreach (var child in entity.Children) { RecursivelySerialize(child, writer); } }
private static PrefabEntity RecursivelyDeserialize(BinaryReader reader) { //read name and init PrefabEntity prefabEntity = new PrefabEntity(reader.ReadString()); //read transform prefabEntity.Position.X = reader.ReadSingle(); prefabEntity.Position.Y = reader.ReadSingle(); prefabEntity.Position.Z = reader.ReadSingle(); //position prefabEntity.Rotation.X = reader.ReadSingle(); prefabEntity.Rotation.Y = reader.ReadSingle(); prefabEntity.Rotation.Z = reader.ReadSingle(); prefabEntity.Rotation.W = reader.ReadSingle(); //rotation prefabEntity.Scale.X = reader.ReadSingle(); prefabEntity.Scale.Y = reader.ReadSingle(); prefabEntity.Scale.Z = reader.ReadSingle(); //scale prefabEntity.OriginOffset.X = reader.ReadSingle(); prefabEntity.OriginOffset.Y = reader.ReadSingle(); prefabEntity.OriginOffset.Z = reader.ReadSingle(); //origin //read mesh & material if (reader.ReadBoolean()) { prefabEntity.Mesh = DataManager.Meshes[reader.ReadString()]; //mesh } if (reader.ReadBoolean()) { prefabEntity.Material = DataManager.Materials[reader.ReadString()]; //material } //read children int childrenCount = reader.ReadInt32(); for (int i = 0; i < childrenCount; i++) { prefabEntity.Children.Add(RecursivelyDeserialize(reader)); } return(prefabEntity); }
private Entity RecursivelySpawnEntities(PrefabEntity prefabEntity, Entity parent) { Entity entity = new Entity(prefabEntity.Name, parent: parent); entity.Transform.Position = prefabEntity.Position; entity.Transform.Rotation = prefabEntity.Rotation; entity.Transform.Scale = prefabEntity.Scale; entity.Transform.OriginOffset = prefabEntity.OriginOffset; if (prefabEntity.Material != null && prefabEntity.Mesh != null) { var renderer = entity.AddComponent <MeshRenderer>(); renderer.Mesh = prefabEntity.Mesh; renderer.Material = prefabEntity.Material; } foreach (var child in prefabEntity.Children) { RecursivelySpawnEntities(child, entity); } return(entity); }