Part of these game systems:
intermediate Save & Load

JSON Save Utility

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

Unity 2022.3+ · 2.0 KB · JsonSaveUtility.cs

How to Use

1

No setup required — JsonSaveUtility is a static utility class

2

Create a serializable data class:

3

[System.Serializable]

4

public class GameData { public int score; public Vector3 position; }

5

Save: JsonSaveUtility.Save(gameData, "slot1")

6

Load: var data = JsonSaveUtility.Load("slot1")

7

Check: JsonSaveUtility.Exists("slot1")

8

Delete: JsonSaveUtility.Delete("slot1")

9

Files saved to Application.persistentDataPath/saves/

Features

  • Save any serializable class to a JSON file with one method call
  • Auto-creates the saves directory if it doesn't exist
  • Load with type safety using generics
  • Exists and Delete methods for save file management
  • GetAllSaveFiles to list available save slots
  • Pretty-printed JSON output for easy debugging

When to Use This

Use for RPGs, roguelikes, simulation games, or any project needing persistent save data across sessions. Supports multiple save slots out of the box. Pair with SimpleInventory or QuestSystem to persist player progress. Prefer this over PlayerPrefs for structured or larger data.

Common Mistakes

Your data class must be marked [System.Serializable] and use only serializable fields — JsonUtility cannot handle dictionaries, properties, or polymorphic types. The file is saved to Application.persistentDataPath, which varies by platform; don't hardcode paths. If Load returns default, check the Console for the warning — the file name might be misspelled or missing the .json extension.

Source Code

JsonSaveUtility.cs
C#
using UnityEngine;
using System.IO;

/// <summary>
/// File-based JSON save/load utility.
/// Saves serializable classes to Application.persistentDataPath.
/// </summary>
public static class JsonSaveUtility
{
    /// <summary>
    /// Save a serializable object to a JSON file.
    /// </summary>
    public static void Save<T>(T data, string fileName)
    {
        string path = GetFilePath(fileName);
        string json = JsonUtility.ToJson(data, true);

        try
        {
            string directory = Path.GetDirectoryName(path);
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);

            File.WriteAllText(path, json);
            Debug.Log("[SaveSystem] Saved to: " + path);
        }
        catch (System.Exception e)
        {
            Debug.LogError("[SaveSystem] Save failed: " + e.Message);
        }
    }

    /// <summary>
    /// Load a serializable object from a JSON file.
    /// Returns default(T) if file doesn't exist.
    /// </summary>
    public static T Load<T>(string fileName)
    {
        string path = GetFilePath(fileName);

        if (!File.Exists(path))
        {
            Debug.LogWarning("[SaveSystem] File not found: " + path);
            return default;
        }

        try
        {
            string json = File.ReadAllText(path);
            T data = JsonUtility.FromJson<T>(json);
            Debug.Log("[SaveSystem] Loaded from: " + path);
            return data;
        }
        catch (System.Exception e)
        {
            Debug.LogError("[SaveSystem] Load failed: " + e.Message);
            return default;
        }
    }

    /// <summary>
    /// Check if a save file exists.
    /// </summary>
    public static bool Exists(string fileName)
    {
        return File.Exists(GetFilePath(fileName));
    }

    /// <summary>
    /// Delete a save file.
    /// </summary>
    public static bool Delete(string fileName)
    {
        string path = GetFilePath(fileName);

        if (!File.Exists(path))
            return false;

        try
        {
            File.Delete(path);
            Debug.Log("[SaveSystem] Deleted: " + path);
            return true;
        }
        catch (System.Exception e)
        {
            Debug.LogError("[SaveSystem] Delete failed: " + e.Message);
            return false;
        }
    }

    /// <summary>
    /// Get all save file names in the save directory.
    /// </summary>
    public static string[] GetAllSaveFiles()
    {
        string directory = Path.Combine(Application.persistentDataPath, "saves");

        if (!Directory.Exists(directory))
            return new string[0];

        string[] files = Directory.GetFiles(directory, "*.json");
        for (int i = 0; i < files.Length; i++)
            files[i] = Path.GetFileNameWithoutExtension(files[i]);

        return files;
    }

    private static string GetFilePath(string fileName)
    {
        if (!fileName.EndsWith(".json"))
            fileName += ".json";

        return Path.Combine(Application.persistentDataPath, "saves", fileName);
    }
}