Quick Start

Summary: Run the sample and make your first save.

Save and restore player progress in Unity with Save System. In the Quick Start sample, you’ll save coins and a level, restart Play mode to verify they were saved, and delete the save to start fresh. You’ll need Unity 2022.3 or later.

Prefer video? Watch the Getting Started tutorial.

1. Set up Save System

Open Tools › Gley › Save System. If the window shows Create settings, click it to create a settings asset with encryption enabled.

Unity’s Save System window with the Create settings button highlighted.
Create the Save System settings asset.

2. Save and load your progress

  1. In the Project window, go to Assets/Gley/SaveSystem/Samples/QuickStart/Scenes/. Open SaveExample-QuickStart.unity and press Play. On the first run, you’ll see 0 coins and level 1.
  2. Press Add Coin and Level Up once each, then press Save.
  3. Stop Play mode and press Play again. The scene loads the saved values when it starts.
  4. Change the values again, then press Load to restore the last save while the scene is running.
  5. Press Delete to remove the sample’s save and return to 0 coins and level 1. Press Load to see the first-run message again.
Quick Start Game view with 0 coins, level 1, and the message No save yet. Using default values.
On the first run, the sample shows 0 coins and level 1.
Quick Start Game view after restarting Play mode, showing 1 coin, level 2, and the message Loaded.
After restarting Play mode, the sample restores 1 coin and level 2.

3. Save and load in your own script

Create a script named SimpleSave.cs, replace its contents with the code below, and add it to any GameObject in your scene. Press Play and the Console shows Times played: 1. Stop Play mode and press Play again to see Times played: 2.

SimpleSave.cs · C#
// Needed for the [Serializable] attribute.
using System;
// Gives access to the Save System API.
using Gley.SaveSystem;
// Gives access to Unity types like MonoBehaviour and Debug.
using UnityEngine;

// The data you want to keep between game sessions.
// [Serializable] lets the Save System turn this class into a save file.
[Serializable]
public class PlayerProgress
{
    // [SerializeField] marks this field to be saved, even though it is private.
    [SerializeField] private int _timesPlayed;

    // Lets other scripts read and change the saved value.
    public int TimesPlayed { get => _timesPlayed; set => _timesPlayed = value; }
}

// Add this script to any GameObject in your scene.
public class SimpleSave : MonoBehaviour
{
    // The name of the save. Use the same name to save and to load the same data.
    private const string SaveName = "player-progress";

    // Unity calls Start once, when you press Play.
    private void Start()
    {
        // Load the data saved under SaveName.
        // If nothing is saved yet, or the save can't be read, CreateDefaultProgress makes new data instead.
        LoadResult<PlayerProgress> loadResult = API.Load<PlayerProgress>(SaveName, defaultValue: CreateDefaultProgress);
        // Take the loaded data out of the result.
        PlayerProgress progress = loadResult.Data;
        // Change the data: add 1 to the number of times played.
        progress.TimesPlayed++;

        // Save the changed data under the same name. The game waits until the save is done.
        SaveResult saveResult = API.Save(progress, SaveName);
        // IsSuccess is true when the data was saved.
        if (saveResult.IsSuccess)
        {
            // Show the new value in the Console window.
            Debug.Log("Times played: " + progress.TimesPlayed);
        }
        else
        {
            // Show why the save failed in the Console window.
            Debug.LogError("Couldn't save: " + saveResult.Message);
        }
    }

    // Creates the starting data for a new player. Load calls it when there is no save to load.
    private PlayerProgress CreateDefaultProgress()
    {
        // A new PlayerProgress starts with 0 times played.
        return new PlayerProgress();
    }
}

PlayerProgress holds the data to save. It’s marked [Serializable], and with the default JsonUtility serializer, its [SerializeField] fields are what gets saved, even when they are private. Add your own fields to it as your game grows.

API.Load reads the data saved under SaveName. If nothing is saved yet or the save can’t be read, it calls CreateDefaultProgress and returns the new data in loadResult.Data. defaultValue: is a named argument that lets you skip the optional options parameter. A missing save returns SaveStatus.NotFound. If there’s an error, check loadResult.Status and loadResult.Message for details.

API.Save waits for the write to finish. Check IsSuccess before reporting success, and use Message to see what went wrong. Pass the same save name to every call, and keep that name stable because existing saves are stored under the original name.

The Quick Start sample uses the same calls in QuickStartExample.cs, with Save, Load and Delete wired to buttons.

Delete when the player resets their progress

C#
SaveResult result = API.Delete(SaveName);

After a successful delete, the next API.Load returns the values from your default method again. If the delete fails, keep the current values and use result.Message to report what happened.

Next steps

  • Choose storage, serialization, and platform settings in the Setup Guide.
  • Open SaveExample.unity in Assets/Gley/SaveSystem/Samples/SaveExample/Scenes/ to see player data, an inventory and an object’s position saved across three slots, with async saving, slot metadata, deleting a slot and upgrading an older save on load.
  • Look up method parameters, result types, and async alternatives in the API Reference.
On This Page