Vault Save System

Examples

Player Save / Load (with components)

// PlayerController.cs
public class PlayerController : MonoBehaviour
{
    [Save] public float health = 100f;
    [Save] public int   coins  = 0;

    public void SaveGame()
    {
        Vault.BeginBatch();
        Vault.Save("player", this, b => b.With<Transform>());
        Vault.EndBatch();
    }

    public void LoadGame()
    {
        var cc = GetComponent<CharacterController>();
        cc.enabled = false;
        Vault.Load("player", this, b => b.With<Transform>());
        cc.enabled = true;
    }
}

Inventory — List of Custom Items

[Serializable]
public class Item
{
    public string id;
    public int    quantity;
}

public class Inventory : MonoBehaviour
{
    [Save] public List<Item> items = new();

    void Awake()
    {
        // Refresh UI after any load
        Vault.OnLoaded += _ => RefreshUI();
    }

    public void Save() => Vault.Save("inv", this, _ => { });
    public void Load() => Vault.Load("inv", this, _ => { });

    void RefreshUI() { /* update item slots from items list */ }
}

Game Settings (ScriptableObject)

[CreateAssetMenu]
public class Settings : ScriptableObject
{
    [Save] public float masterVolume = 1f;
    [Save] public bool  fullscreen   = true;
    [Save] public int   graphicsLevel = 2;
}

// Anywhere in your code:
Vault.SetDrawer("settings");
Vault.Save("settings", mySettings);    // writes to disk
Vault.Load("settings", mySettings);    // restores fields in-place

Multiple Save Slots

// Save to a named slot
public void SaveSlot(int slot)
{
    Vault.SetDrawer($"slot_{slot}");
    Vault.BeginBatch();
    Vault.Save("player", player, b => b.With<Transform>());
    Vault.Save("world.level", currentLevel);
    Vault.Save("world.time",  gameTime);
    Vault.EndBatch();
}

// Load from a named slot (skip if it doesn't exist)
public void LoadSlot(int slot)
{
    Vault.SetDrawer($"slot_{slot}");
    if (!Vault.DrawerExists()) return;

    Vault.Load("player", player, b => b.With<Transform>());
    currentLevel = Vault.Load<int>("world.level", 1);
    gameTime     = Vault.Load<float>("world.time", 0f);
}

// List all slots
string[] slots = Vault.GetAllDrawerNames()
    .Where(n => n.StartsWith("slot_"))
    .ToArray();

Saving Many Objects (Enemies, Projectiles)

// Enemy.cs — static registry
public class Enemy : MonoBehaviour
{
    public static List<Enemy> All = new();
    [Save] public float health;
    [Save] public int   prefabIndex;

    void OnEnable()  => All.Add(this);
    void OnDisable() => All.Remove(this);
}

// Save all enemies in one call
Vault.Save("enemies", Enemy.All, b => b.With<Transform>());

// Restore enemies on load
foreach (var e in new List<Enemy>(Enemy.All))
    Destroy(e.gameObject);

foreach (var data in Vault.ReadInstances("enemies"))
{
    int idx = data.Get<int>("prefabIndex");
    var go  = Instantiate(enemyPrefabs[idx]);
    data.ApplyTo(go.GetComponent<Enemy>());
    data.ApplyTo(go.GetComponent<Transform>());
}