- Home /
How to refference a Prefab in a saving/loading system?
Sorry for the repost but i posted the Question in the wrong Space. I really need an answer because this is for a Uni assignment. So iam thankful for any advice.
Iam trying to add a loading/saving system to my game right now. Most things work with numbers so thats easy to save and load. What iam struggeling with right now is how to save or load a Prefab, so what type of GameObject is supposed to be instantiated in the load. I guess using an Object tag to refference it dosnt really work but i havnt been able to find a working method for it.
Code:
public void Save(BinaryWriter writer)
{
writer.Write(terrainTypeIndex);
writer.Write(elevation);
if (hasChild == true)
{
ObjectTag thisObject = gameObject.GetComponentInChildren<ObjectTag>();
float positionX = thisObject.transform.position[0];
float positionY = thisObject.transform.position[1];
float positionZ = thisObject.transform.position[2];
float rotationX = thisObject.transform.rotation[0];
float rotationY = thisObject.transform.rotation[1];
float rotationZ = thisObject.transform.rotation[2];
string objectTag = thisObject.tag;
writer.Write(hasChild);
writer.Write(positionX);
writer.Write(positionY);
writer.Write(positionZ);
writer.Write(rotationX);
writer.Write(rotationY);
writer.Write(rotationZ);
writer.Write(objectTag);
}
}
public void Load(BinaryReader reader)
{
terrainTypeIndex = reader.ReadInt32();
elevation = reader.ReadInt32();
hasChild = reader.ReadBoolean();
RefreshPosition();
if (hasChild == true)
{
string objectTag = reader.ReadString();
GameObject thisObject = Instantiate<ObjectTag>(GameObject.FindGameObjectsWithTag(objectTag));
Vector3 objectPosition = new Vector3(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32());
thisObject.transform.position = objectPosition;
Quaternion objectRotation = new Quaternion(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), 0);
thisObject.transform.rotation = objectRotation;
}
}
Comment