This sample uses Unity's 2D Platformer Microgame learning template to demonstrate how you can incorporate Easy Save into a project using the Manager approach to saving and loading. This is where one Component handles all of the saving and loading for your scene, rather than getting objects to save themselves.
Pros
- Can be more computationally efficient as it usually requires saving fewer keys to a file.
- Doesn't require everything to have a unique ID (as arrays of objects are used instead), which can make things simpler.
- Can be easier to debug as all of your save and load logic is in one place rather than spread across the scene.
- The code can look more complex to beginners.
- Because it's reliant on arrays, issues can occur if objects are reordered or removed.
In order to set up this example you will need to do the following:
- Ensure that you're using Unity 2020 or 2021, as Unity 2022 doesn't support Learning templates.
- Create a new '2D Platformer Microgame' project (Unity Hub > New Project > Editor Version: 2020 or 2021 > Learning > 2D Platformer Microgame > Create Project).
- Import Easy Save from the Package Manager (Window > Package Manager > My Assets > Easy Save > Import). You should also ensure that it's updated to the latest version of Easy Save.
- Download the UnityPackage attached below and import the package attached below by double-clicking it, or by going to Assets > Import Package > Custom Package.
- Open the Scene Assets > Easy Save > Examples > 2D Platformer Microgame - Manager Approach > EasySaveSampleScene.
- Go to File > Build Settings and press Add Open Scenes.
This is due to a bug in Unity's Tutorial Framework. Although this error is harmless, you may want to go to Package Manager > Unity Registry > Tutorial Framework and select Remove to prevent it from appearing.ArgumentException: An item with the same key has already been added.
What changes were made
The following changes were made to the default scene:
- An Easy Save Manager was added to the scene by going to Tools > Easy Save 3 > Add Manager to Scene. This enables us to save and load things by reference.
- A GameObject named Save Manager was created and a SaveManager script added.
- Save and Load buttons were added to UI Canvas/Buttons, and these were hooked up to the Save() and Load() methods of the SaveManager script.
- The Load button was made non-interactable by default.
We recommend taking a look at SaveManager.cs in Assets > Easy Save 3 > Examples > 2D Platformer Microgame - Manager Approach. This is fully commented and explains exactly how each aspect works.
As an overview, the following happens in this scene:
- When the scene loads, it initialises the SaveManager's variables and then checks for save data.
- If there is save data, it will load the variables from the data and enable the Load button.
- When the user presses the Save button (located by pressing Esc), it will save the required variables to the default file and enable the Load button.
- When the user presses Load, it reloads the current scene. This in turn takes us back to Step 1.