- Home /
What is the best/easiest way to save a game (or scene) at runtime?
Ok, so Im wondering what is the best/easiest way to save my game (or entire scene) during runtime so it can be loaded later? I know about Player Prefs and that it wouldn't be the best way to save my game because of the amount of data I would have to store, also Player Prefs can be modified easily. Im looking for a more secure way to save data than Player Prefs. Ive looked into using a Binary Formatter and Serialization and that seems like that is going to have to be the way I go but before I spend a lot of time setting up saving via the Binary Formatter/Serialization I want to know is this the best option?
My initial thinking was: "Can't I just save the whole scene as a new scene and open it later using EditorSceneManager.SaveScene
and EditorSceneManager.LoadScene
?" I found that I couldn't fully test this method unless I built and ran my game.
So Im asking do I really have to go through every object that has changed in anyway (positions, rotations, bools, list values... ect) during runtime and save what has changed and Serialize the values with a Binary Formatter? Then Load and plug those values back into a new instantiated gameobject when the game loads? Is saving and loading a whole scene a possible option? Any help is appreciated!
Answer by ryanmillerca · Jul 19, 2019 at 05:04 PM
This sort of thing has been asked before, have a look at this link: https://answers.unity.com/questions/1315394/how-to-save-and-load-a-level-by-serialization.html There's no straightforward way to use Unity's Scene serialization at runtime to create a save game system. Additionally, the EditorSceneManager tools don't exist in a build. Even if there was, there's a lot of data in each scene that you would not want to go through saving/loading every time. The preferred method is to create a data structure that only includes everything that needs to be saved. The most common approach is to create a save file class that gets serialized via Binary/JSON/XML and saved to disk in Application.persistentDataPath. You can use multiple save files, if you wish - one for player stats, one for each level, etc.