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.
Adding a level to RetroAdventure
- create a new project in Unity 2019.4.22f1
- import Corgi Engine v6.7
- duplicate RetroAdventure3, name it RetroAdventure6
- add that newly created scene to the build settings
- open RetroAdventureLevelSelection, under the Content node, duplicate the RetroAdventureLevel5 node, change its ItemTitle’s text to “Level6”, on its RetroAdventureLevel comp, change scene name to “RetroAdventure6”
- on the RetroAdventureProgressManager (in that scene and on the prefab), add a 6th scene, set its name to “RetroAdventure6”
- press play, go through the levels
- at the end of lvl 5 you get redirected to the level selection (that’s normal, we didn’t change the exit scene in that level)
- play level 6, collect some stars in it, return to level selection, enjoy your newly collected stars
Recreating a RetroAdventure-like progress
Creating new scenes
- in a fresh install of Corgi Engine v7.2.1, in Unity 2019.4.28f1
- duplicate the MinimalLevel, name it Progress1
- open it
- select the RetroAdventureProgressManager prefab, change its 5 scene names to Progress1, Progress2, Progress3, Progress4 and Progress5
- drag the prefab in your scene
- drag a RetroAdventureLevelEnd prefab in your scene, position it at -4,-4.35,0
- change its FinishLevel/LevelName to Progress2
- select the ButtonLevelSelection’s Background, set its LevelSelector’s Level Name to RetroAdventureLevelSelection
- remove the UICamera from the scene, drag a RetroAdventureUICamera into it instead
- disable its InventoryCanvas child
- save the scene
- duplicate it, rename the copies Progress2, Progress3, Progress4 and Progress5
- open them each to edit their RetroAdventureLevelEnd and change the destination scenes to n+1, have Progress5’s be RetroAdventureLevelSelection
- add all 5 levels to the build settings (file > build settings > drag and drop your new scenes in the list)
Creating a new level selection scene
- duplicate the RetroAdventureLevelSelection scene, name it ProgressLevelSelection
- for each of the buttons (at the RetroAdventureLevel component level), change the Scene Names to Progress1, 2 etc
- save the scene
- add it to the build settings
- click the “Tools > More Mountains > Reset all progress” menu item
- press play, notice all levels are locked except level 1
- click on level 1’s play button
- in Progress1, move right to touch the level’s exit
- click on the “next level” button
- in Progress2, move right to touch the level’s exit
- click on the “next level” button
- in Progress3, press escape, then Back to level selection
- notice all 3 first levels are unlocked
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
Persisting other things
The Inventory Engine also lets you persist more than just inventories. In fact, it lets you persist any object you want, meaning that when exiting a scene you can save the state of an object, and the next time you come back to it, whether it’s in the same session, or days later in a different session, that object’s state will be loaded and restored.
You can learn more about how to do that in the Inventory Engine’s documentation.