PlayerPrefs Save Helper

Type-safe PlayerPrefs wrapper with support for bool, Vector3, Color, and any serializable object via JSON.

File
SaveHelper.cs
Unity
2022.3+
Level
beginner
Size
1.6 KB · 92 lines
Added
2026-03-12
License
MIT
View source

No APIs that Unity 6.0 to 6.6 made obsolete or removed. How this is checked

What it adds to your GameObject

A static helper class: call it from your own code, no GameObject needed.

Inspector
Script SaveHelper

No serialized fields. This is a static helper or a base class: you call it from code rather than configure it in the Inspector.

When to use it

Ideal for storing small amounts of data like settings, preferences, high scores, and checkpoints in casual games, mobile titles, and prototypes. Best for quick prototyping when you don't need file-based persistence. For larger save data or multiple save slots, use JSON Save Utility instead.

Setup

  1. No setup required — SaveHelper is a static utility class
  2. Save bool: SaveHelper.SetBool("muted", true)
  3. Save Vector3: SaveHelper.SetVector3("checkpoint", transform.position)
  4. Save object: SaveHelper.SetObject("settings", mySettings)
  5. Load: var pos = SaveHelper.GetVector3("checkpoint")
  6. Check: SaveHelper.HasKey("checkpoint")
  7. For file-based saves with multiple slots, use JSON Save Utility instead

Features

  • Type-safe wrappers for bool, Vector3, and Color via PlayerPrefs
  • Generic SetObject/GetObject for any serializable type using JSON
  • HasKey, Delete, and DeleteAll utility methods
  • Immediate Save() to flush data to disk
  • Static class — no MonoBehaviour or setup required

Public API

What other scripts can call on SaveHelper.

MemberReturnsDescription
static SetBool(string key, bool value)void
static GetBool(string key, bool defaultValue = false)bool
static SetVector3(string key, Vector3 value)void
static GetVector3(string key, Vector3 defaultValue = default)Vector3
static SetColor(string key, Color value)void
static GetColor(string key, Color defaultValue = default)Color
static HasKey(string key)boolCheck if a key exists.
static Delete(string key)voidDelete a specific key.
static DeleteAll()voidDelete all saved data.
static Save()voidSave changes to disk immediately.

Common mistakes

PlayerPrefs has platform-specific size limits (typically 1 MB) — don't use it for large save data. SetObject only works with types that JsonUtility can serialize, meaning no dictionaries or polymorphic types. Always call SaveHelper.Save() if you need data written immediately; otherwise it only flushes on application quit.

Source code

SaveHelper.cs, 92 lines, MIT licensed. Paste it into a new C# file of the same name.

SaveHelper.cs
C#
using UnityEngine;

/// <summary>
/// Type-safe PlayerPrefs wrapper. Supports bool, Vector3, Color,
/// and any serializable type via JSON.
/// </summary>
public static class SaveHelper
{
    // ---- Bool ----
    public static void SetBool(string key, bool value)
    {
        PlayerPrefs.SetInt(key, value ? 1 : 0);
    }

    public static bool GetBool(string key, bool defaultValue = false)
    {
        return PlayerPrefs.GetInt(key, defaultValue ? 1 : 0) == 1;
    }

    // ---- Vector3 ----
    public static void SetVector3(string key, Vector3 value)
    {
        PlayerPrefs.SetFloat(key + "_x", value.x);
        PlayerPrefs.SetFloat(key + "_y", value.y);
        PlayerPrefs.SetFloat(key + "_z", value.z);
    }

    public static Vector3 GetVector3(string key, Vector3 defaultValue = default)
    {
        return new Vector3(
            PlayerPrefs.GetFloat(key + "_x", defaultValue.x),
            PlayerPrefs.GetFloat(key + "_y", defaultValue.y),
            PlayerPrefs.GetFloat(key + "_z", defaultValue.z)
        );
    }

    // ---- Color ----
    public static void SetColor(string key, Color value)
    {
        PlayerPrefs.SetFloat(key + "_r", value.r);
        PlayerPrefs.SetFloat(key + "_g", value.g);
        PlayerPrefs.SetFloat(key + "_b", value.b);
        PlayerPrefs.SetFloat(key + "_a", value.a);
    }

    public static Color GetColor(string key, Color defaultValue = default)
    {
        return new Color(
            PlayerPrefs.GetFloat(key + "_r", defaultValue.r),
            PlayerPrefs.GetFloat(key + "_g", defaultValue.g),
            PlayerPrefs.GetFloat(key + "_b", defaultValue.b),
            PlayerPrefs.GetFloat(key + "_a", defaultValue.a)
        );
    }

    // ---- Generic JSON ----

    /// <summary>
    /// Save any serializable object as JSON to PlayerPrefs.
    /// </summary>
    public static void SetObject<T>(string key, T obj)
    {
        string json = JsonUtility.ToJson(obj);
        PlayerPrefs.SetString(key, json);
    }

    /// <summary>
    /// Load a serializable object from PlayerPrefs JSON.
    /// </summary>
    public static T GetObject<T>(string key, T defaultValue = default)
    {
        string json = PlayerPrefs.GetString(key, "");
        if (string.IsNullOrEmpty(json))
            return defaultValue;

        return JsonUtility.FromJson<T>(json);
    }

    // ---- Utility ----

    /// <summary>Check if a key exists.</summary>
    public static bool HasKey(string key) => PlayerPrefs.HasKey(key);

    /// <summary>Delete a specific key.</summary>
    public static void Delete(string key) => PlayerPrefs.DeleteKey(key);

    /// <summary>Delete all saved data.</summary>
    public static void DeleteAll() => PlayerPrefs.DeleteAll();

    /// <summary>Save changes to disk immediately.</summary>
    public static void Save() => PlayerPrefs.Save();
}

Next step

JSON Save UtilityFile-based JSON save/load utility. Save any serializable class to persistent storage with one method call.