JSON Save Utility
File-based JSON save/load utility. Save any serializable class to persistent storage with one method call.
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.
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
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.
Setup
- No setup required — JsonSaveUtility is a static utility class
- Create a serializable data class:
- [System.Serializable]
- public class GameData { public int score; public Vector3 position; }
- Save: JsonSaveUtility.Save(gameData, "slot1")
- Load: var data = JsonSaveUtility.Load<GameData>("slot1")
- Check: JsonSaveUtility.Exists("slot1")
- Delete: JsonSaveUtility.Delete("slot1")
- 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
Public API
What other scripts can call on JsonSaveUtility.
| Member | Returns | Description |
|---|---|---|
static Exists(string fileName) | bool | Check if a save file exists. |
static Delete(string fileName) | bool | Delete a save file. |
static GetAllSaveFiles() | string[] | Get all save file names in the save directory. |
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, 116 lines, MIT licensed. Paste it into a new C# file of the same name.
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);
}
}