- Home /
Save and load objects created at runtime
How would I go about saving objects in scene that are created at runtime and then load them back in? I looked at Brackeys tut on saving objects position health etc, but I wondered how I would go about saving object created at run time, like MineCraft or Warcraft 3, or Kefir!'s last day on earth... they can save a map with objects created at runtime. A question like this was asked here, but no one answered... Any help is immensely appreciated.
When you say created at runtime, do you mean actual meshes generated or prefabs instantiated?
For prefabs instantiated, I add all possible loadable prefabs into a list, then save the names and info (position, rotation, any other data) to a json file, then instantiate them back in like this:
for (int i = 0; i < database.fullObjectLibrary.Count; i++)
{
if (database.fullObjectLibrary[i].name == objectData.objectName)
{
GameObject newObject = Instantiate(database.fullObjectLibrary[i], objectData.objectPosition, objectData.objectRotation);
newObject.name = objectData.objectName;
database.sceneObjects.Add(newObject.transform);
}
}
prefabs instantiated, thank you for responding
While I can't provide my entire codebase for saving and loading here, I would suggest looking into JSon serialization and the above code. Hope this helps!
Your answer
Follow this Question
Related Questions
Can't find some functions like 'OnParticleCollision'. 1 Answer
Detect mouse collision over an object 0 Answers
Refrencing Monobehaviour in the Editor 3 Answers
Handle vr content on android created by using unity 3D ? 0 Answers
Queue animations 0 Answers