- Home /
Best way to save and load all objects and their parameters in a scene.
I'm trying to read up on serializable. But I was wondering if unity has a simple way to save every gameobject/ including all those instantiated and their parameters/scripts with references to a file. And how to load that scene state?
Is there an easy way to do it without rewriting all the code so that every game object that gets instantiated gets put into a list of gameobjects that is serialized?
Would there be a simple way to collect all gameobjects currently in a scene and save/load them? Does anyone have a clear tutorial on this?
Hoo boy. No, Unity doesnt have a (built-in) feature like that. There are commercial assets on the asset store that can do what you want, however in the end I scripted my own taking some $$anonymous$$or inspiration from Unity Serializer. It was a long, arduous piece of work but I'm pretty happy with it. If I get the neccessary motivation and time in the next few days I might post a lengthy explaination of the whole thing. Until then, I can only dangle a carrot in front of your face, so to speak ;) Oh, and after extensive research I can honestly say that there are no all-encompassing tutorials for serializing whole scenes, and there is virtually zero recent information on how to deal with $$anonymous$$onoBehaviour scripts in that regard.
Oh shi- thanks for that honest answer. I'm still keeping my fingers crossed that someone has done a simple (collect all gameobjects - serialize them - save them) script I can drag and drop onto a button. Here's hoping.
I am also looking for a decent file browser/chooser written to use the new unity 4.6+ UI system. However I can't find a pre-written one. $$anonymous$$ight follow this up with a new question to that effect.
Hi, I would like to ask if you would be willing to give advise on how you managed to serialise a scene, I'm trying something similar, but not sure how that could be done.
Unity Serializer theoretically can do collect all gameobject, serialize them, and save them. However, support has sadly dried up for years now, and it is fairly complex code with critical errors that never got corrected, but an excellent starting point for scripting one's own serialization system. There's no other free asset that does that, at least that what my research has brought up (or didn't bringup, really). The reason saving & loading scenes in Unity is difficult is because it requires insight into advanced thematics like Reflection, per-field-writing, and ISerializationSurrogates for all the Unity-specific things that are serializable for Unity, but not serializable in regards to the respective .NET functions.
$$anonymous$$aybe I should reword my statement about the difficulty: It's not that hard to code a working serialization system, but one that does most of the work for you, without the user having to create all sorts of container classes and variable associations him or herself is hard. Hmm.. this all sounds very technobabble so just stay tuned and I'll post how I do things.
Any progress with this? I have made a very complex game with lots of characters walking around each instantiated with their own individually randomly chosen materials.
I'd like to save their script properties their locations, etc. But for sometime that seems so essential to game design there is nothing available only tutorials on how to save individual serialized simple types.
I tried creating a serializable data class with gameobject lists inside it. But it wouldnt' serialize.
I use the binaryformatter. https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading
That video tutorial should give you the basic idea from there. I use that along with my own component (script) on the objects i save so that I know what they are (for an inventory system I designed).
Hope that helps a bit
I released an asset a couple of months ago, sorry I didn't bookmark this thread, otherwise I would have updated it.
Here is the link:
can i ask you something sir @Cherno . I have a problem with the SerializeHelper . When i grab the canvas and make it a prefab my script doesn't follow it . i mean in my canvas i have a UI Button and it has an OnClick() code . But when i make my canvas a prefab the code disappear . help please
Answer by xAdamQ · Aug 02, 2017 at 03:41 PM
If you want to save data use PlayerPrefs class.
You can also select all GameObjects in The scene and copy them to current or another scene.
Answer by J_LTD · Aug 02, 2017 at 04:30 PM
No, unity does not have a simple way to do this, you will have to use the unity serializer or code your own.
Answer by getloader · Nov 06, 2018 at 09:23 AM
This is disappointing. You can save scenes in the editor all they had to do is allow scene state to be saved at runtime. I don't know maybe it's not simple. Other game engines like renpy or rpg maker comes with save system out of the box but I guess argument can be made that unity is not limited to one type of game like the other engines. PlayerPref is just a no no for saving general game data other than storing game settings (audio, graphics , controls etc). Serialization seems to be the only way. But if your game is more than a few classes big this gets tedious. Maybe I am doing something wrong hope experts will show how they do it. Not only is it lot of work if you have lots of things to save but also if you have something to add later it is a lot of extra work.
Your answer
Follow this Question
Related Questions
Bool array to binaryformatter method 1 Answer
Save Game loading Deserialization from disk 1 Answer
Serializing Properties (JSON saving and loading system) 2 Answers
"Unbroken Reference" problem when using a custom Editor to Save/Load a ScriptableObject Asset 0 Answers
Deserializing data with XML Serializer 0 Answers