- Home /
Dynamically saving and loading player-created prefabs
Hi Unitarians!
In my game, the player can create their own projects. Using already-made block like prefabs, players can make their own unique combination of the prefabs, like Minecraft. The players can then save their model(s) in a singular project to their hard drive. Then, the next time they open the game, I want them to be able to open the file and continue working on it. The file will essentially be a combination of hundreds of prefabs that the user combined together in their own fashion.
How would I do this?
Using playerprefs doesn't work here, and I'm not sure how XML Serialization would work here, if at all.
Thanks in advance!
Think about whata prefab like this really is. It is essentially a GameObject that has a couple of components. So what you would need to do is define a custom class that holds all the required information, so when instantiating or loading to edit a prefab you can take an empty gameobject and add all the components with the right values to it. The simplest way would be to just create a sub-class for each possible component that reflects the variables this component has, and make it part of the prefab class. The advanced way would be to use System.Reflection to go treat components as objects, and go through each field of an object and save / load the data, for example by using a dictionary that holds the field name as key and the value as object.
I have used this method for my save & load system called SerializeHelper, take a look:
SerializeHelper - Free save and load utility. (De)Serialize all objects in your scene.
Thank you! I will check this out. I'm not sure I understand your methodology exactly. Why are you saving components as objects, and how exactly are you able to save these objects onto the hard-drive?
Components are saved as objects because in order to save them to file, they have to be serialized and the Type component is not serializable. Savin files is done via System.IO using a BinaryFormatter.
Your answer
Follow this Question
Related Questions
How do you save a player's prefered resolution in Unity 2017.1.1f? 0 Answers
Saving and Loading Various Things 1 Answer
Distribute terrain in zones 3 Answers
webgl saving. 1 Answer
Unity 3d C# saving and Loading Values 2 Answers