Five-minute quick start

Save a number, a class, a list and a dictionary, read them back, and see the result the next time the game runs.

Get started NexusForge Persistence 1.0.0 Updated

1. Save a value#

csharp
using UnityEngine;
using PersistKit;

public class ScoreKeeper : MonoBehaviour
{
    private int _score;

    private void Start()
    {
        _score = Persist.Load(class="tok-str">"score", 0);     // 0 the first time the game ever runs
        Debug.Log(class="tok-str">"Welcome back. Score: " + _score);
    }

    public void AddPoint()
    {
        _score++;
        Persist.Save(class="tok-str">"score", _score);
    }
}

That is the whole beginner path. There is no manager to add, no Awake ordering to get right, and no "is this the first run?" flag to keep: the default you pass to Load is what a new player gets.

2. Save your own data#

Mark a class [Serializable] and save it like any value. Nested classes and lists of them work too:

csharp
[System.Serializable]
public class PlayerData
{
    public string name = class="tok-str">"hero";
    public int level = 1;
    public Vector3 position;
    public List<string> quests = new List<string>();
}

Persist.Save(class="tok-str">"player", new PlayerData());
PlayerData player = Persist.Load(class="tok-str">"player", new PlayerData());

A collection can also be saved on its own, with no wrapper class:

csharp
Persist.Save(class="tok-str">"levels", new List<int> { 1, 2, 3 });
Persist.Save(class="tok-str">"scores", new Dictionary<string, int> { { class="tok-str">"ada", 10 } });
Persist.Save(class="tok-str">"grid", new int[4, 4]);            // arrays, lists, dictionaries and the nestings of them

3. React to a problem#

Saving and loading return a result instead of throwing, because a full disk is a normal condition and not a crash:

csharp
var result = Persist.Save(class="tok-str">"score", 1500);

if (!result.Ok)
{
    Debug.LogWarning(result);          // class="tok-str">"PK-STORAGE-001: PersistKit couldn't save your data (…)."
    Debug.LogWarning(result.NextStep); // what to try next
}

Load gives you your default and sets Persist.LastResult when something went wrong, so a damaged file can never stop the game from starting.

4. Check it yourself#

  1. Press Play. Watch the Console say Welcome back. Score: 0.
  2. Call your save (or use the window: Tools → PersistKit → Open Save Folder shows the file).
  3. Stop and press Play again. The value is back.

5. Save a whole scene object without code#

Add PersistKit Behaviour to an object, tick the parts you want remembered in the Inspector, and press Play. The no-code guide walks through it, and choosing individual fields explains the Save and Load columns.

Where saves live#

A PersistKit folder inside Unity's per-platform persistent data folder, in a file called save.json, with the previous versions kept beside it as backups. Tools → PersistKit → Open Save Folder opens it. Persist.SaveFolder tells you the path in code, and Persist.SetSaveFolder(path) moves it.

Next#

Something wrong on this page? Every page here describes behaviour that is checked in the repository. If a page and the package disagree, the package wins.

Report a documentation problem · Error codes · Platforms and limitations