Part of these game systems:
JSON Save Utility
File-based JSON save/load utility. Save any serializable class to persistent storage with one method call.
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
7
Check: JsonSaveUtility.Exists("slot1")
8
Delete: JsonSaveUtility.Delete("slot1")
9
Files saved to Application.persistentDataPath/saves/
Source Code
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);
}
}