- Home /
I need to save and load GameObjects. How do I do this?
I have tried saving and loading through playerprefs, but I don't want the save to be stored in the registry. I tried serialization, but it doesn't work for gameObjects. I have even tried the Unity Serializer, but it does not work either since in my game, objects are being created and destroyed all the time. In my game, the player places newly instantiated wall sections in a multidimensional list. I need to be able to save where all the wall sections are with their rotations and materials. Is this even possible?
You probably don't want to save the gameobjects themselves - that's a lot of data. If you're just saving walls, you save the information you need about them, and create a format to save that to a file.
It looks to me like your walls are defined by their position, their rotation, and which of probably some predefined range of materials they use. That's 6 float coordinates, and one int specifying the material. Should not be to hard to prototype out a working solution.
Answer by Cherno · Oct 06, 2014 at 12:25 AM
Edit: Link to my free Save & Load Utility: SerializerHelper
Unity Serializer should work ok with instantiated objects (if you give the prefabs a Prefab Identifier). It seems to break down then and now, however.
It's true that you can't serializer things like GameObjects, Vector3s and Materials. However, workarounds are usually pretty easy. A position and rotation can just be represented as three integers (like posX, posY, posZ), Materials? Just save the Material name and/or file path in a string, and load it again when loading a saved game with Resource.Load. A GameObject can be represented with an unique ID int, and to load an instantiated GO you can save it's name and then look up this name in a dictionary that lists all names and the corresponding GO filenames in your project folder.
These are just examples, and it's how I do it and it worked well thus far. The multidimensional list needs to be flattened when serializing it, of course, and made multi-dimensional again after deserializing, but that's also easy.
I think I will have to end up saving strings of the prefab name, materials, positions, and rotations just like you said. Unity Serializer seems to not work for my setup and your suggestion is the next best thing.
A GameObject can be represented with an unique ID int, and to load an instantiated GO you can save it's name and then look up this name in a dictionary that lists all names and the corresponding GO filenames in your project folder.
Could you elaborate on this?
I encourage you to take a look at my free save & load utility:
It is designed to $$anonymous$$ch users the basics of saving & loading any kind of data and is a great base framework to add your own functionality.
Answer by chrismcrae5712 · Oct 06, 2014 at 12:18 AM
This isn't really an answer just a suggestion. I am currently in the works for the same setup. The best solution I have found is if you're making a game where players cannot access files then you should store everything in a database. If you wish to store GameObjects, then the best solution would be to store all the data for the GameObject's via MySQL database or similar. Goto youtube and search Unity3d with MYSQL and you'll get a bunch of tutorials on how to do this some even with gameobjects. The other way (if u dont care if players can access the files) then instantiate prefabs and make the gameobjects into prefabs.
Answer by Voxel-Busters · Aug 08, 2015 at 10:05 PM
You can directly serialize GameObject using our plugin Runtime Serialization for Unity. Its not just another serialization plugin which works only on custom c# objects. But what makes it special is its capablity to serialize Unity Objects like GameObject, MonoBehaviours, Textures, Prefabs etc. For more info about supported list, please check this link.