Once your game starts to scale, you’ll likely have more than one scene. In Unity, data is lost (unless specified otherwise) when going from one scene to the other. So you’ll likely want to save things. This page covers the various ways you can do so in the engine, and covers the parts where it’s already done for you.
Automatic save in the engine
Some systems inside the engine already handle their own save and load, so you don’t have to worry about them :
- sound settings
All of these use the MMSaveLoadManager class to do so. You can of course check out their code to see how they do it, if you want to build similar systems. And if you want to clear the save files associated to these systems, you can use Unity’s Tool menu, then go to MoreMountains > Reset All Achievements, Reset All Progress, or Reset All Saved Inventories.
Sometimes you just want a Character that persists across scenes, without resetting its state, health, components, values, etc. For this, you can simply add the CharacterPersistency ability to your character. When it’ll travel from scene to scene, it’ll keep all its attributes, and all the changes you’ve made to it, and it’ll be passed to the new scene’s LevelManager to be used as the scene’s playable character.
Maybe you’ll also want to save progress. What actually is progress? That’s probably very unique to your game. Maybe you’ll want to keep track of score, collected mushrooms, amount of visited levels, etc. For this reason there is no universal save system in the engine, as all games will by nature have different needs when it comes to saving stuff. That said, you’ll find an example of progress management in the RetroAdventure folder. In it, the RetroProgressManager class is responsible for saving and loading progress across levels, and you can of course look at how it works to build your own system.
You can either copy this one into your own progress manager, or extend it if your needs are close enough. The Retro Adventure Progress Manager uses serializable classes to save data to file and read it later. It waits for LevelComplete events, and when it gets one, it saves what level’s been saved, which ones are unlocked, how many lives are left to the player, and how many stars (and which ones) were collected. When combined with the inventory and achievements systems (which handle their own save/load mechanisms), you’ve got a complete solution to save all progress in your game.
The engine comes with a simple class to handle basic save and load needs. The MMSaveLoadManager is a static class that allows the save and load of objects in a specific folder and file. It will let you save and load any object to a file. You can find examples of it in action in the Inventory and MMAchievementManager classes, for example.
To save an object, simply call :
MMSaveLoadManager.Save(TestObject, FileName+SaveFileExtension, FolderName);
Then to load it :
TestObject = (YourObjectClass)MMSaveLoadManager.Load(typeof(YourObjectClass), FileName + SaveFileExtension, FolderName);
There are also helper methods to delete saves, save folders, etc. Don’t hesitate to check out the class, it’s fully commented, as usual. You can also specify what IMMSaveLoadManagerMethod the system should use. By default it’s binary but you can also pick binary encrypted, json, or json encrypted. You’ll find examples of how to set each of these in the MMSaveLoadTester class