Infinite Save Scripting
Scripting an infinite save system.
Step 1: Create a Script
Create a script or multiple scripts that will handle all save menu functionality. For this example, we will create a single script called InfiniteSavesExample.cs. Start the script by adding the member variables that can be set from the inspector.
public class InfiniteSavesExample : MonoBehaviour
{
/// <summary>
/// The save slot prefab.
/// </summary>
[SerializeField]
private Button saveSlotPrefab;
/// <summary>
/// A button that creates a new save state.
/// </summary>
[SerializeField]
private Button createSaveButton;
/// <summary>
/// A button that deletes all save states.
/// </summary>
[SerializeField]
private Button deleteAllButton;
/// <summary>
/// Parent transform of save slots.
/// </summary>
[SerializeField]
private Transform content;
/// <summary>
/// Text that displays the time elapsed.
/// </summary>
[SerializeField]
private Text timeElapsedText;
/// <summary>
/// A toggle that controls the save/load mode. If toggled on, clicking on a save slot will load it. If toggled off,
/// clicking on a save slot will overwrite the save.
/// </summary>
[SerializeField]
private Toggle modeToggle;
}Remember to add a GameObject to the scene and add this script to it. At this point, we should have everything to populate the inspector properties. Drag and drop each component to the relevant property.

Step 2: Other Script Members
We will create other members that won't be visible in the inspector.
If load mode is true, we will make the save slots load a save. If it's false (save mode), we will make the save slots overwrite a save.
Step 3: Initialize ESave
You can either add the ESave Initializer component to your scene or add the code below to your script.
The idea is just to initialize ESave on awake and terminate it on exit.
Step 4: Instantiate Existing Saves
When we enter play mode, we need to first load any existing saves that the player may have made in a previous session. We can do this in the Start method.
Also, save slot UI objects will need to be instantiated for this, so we'll create a method called CreateNewSaveSlot that will do just that.
Step 5: Increment Time
In the Update method, we will increment the time elapsed. This will be the only data that will be saved and loaded for this example.
Step 6: Saving and Loading Data
We will create 3 methods for saving and loading. The first one will load the data from a save state.
The second one will save (or overwrite) the data in the save file.
The third one will be used by the save slots and will load or overwrite the data depending on the mode.
Step 7: Create New Save
We have methods that require a save state, but no save state is being created yet. A save state should be created when the 'Create New Save' button is pressed, along with a save slot.
So, let's create a method for this.
Step 8: Delete All Saves
At this point, we can create saves, but we can't delete them. Let's add some code that will delete all saves that exist.
Step 9: Final Button Events
We still need the create save button and the delete button to execute the correct methods on-click. This can be done by adding an Awake method. Exclude the initialization code if your using the ESave Initializer component.
Done! You can now test it in play mode.
Last updated