JsonSaveLoadService
is a flexible and powerful service designed for saving and loading data in Unity applications. It supports various data types including primitives, custom objects, and collections like Dictionary, HashSet, List, Queue, and Stack. This service ensures cross-platform compatibility (PC, Android, Web) and includes functionality for handling default values when loading data for the first time.
This service requires the Newtonsoft.Json library for JSON serialization and deserialization. Specifically, it depends on the Unity package com.unity.nuget.newtonsoft-json
version 3.2.1
.
To use JsonSaveLoadService
in your project, you must first ensure that Newtonsoft.Json is properly installed. Follow these steps to install the Newtonsoft.Json package through Unity's Package Manager:
- Open your Unity project.
- Navigate to
Window
>Package Manager
. - Click the
+
button in the top left corner of the Package Manager window. - Select
Add package from git URL...
. - Enter
com.unity.nuget.newtonsoft-json@3.2.1
and clickAdd
. - Unity will download and install the Newtonsoft.Json package.
Alternatively, you can manually edit your project's Packages/manifest.json
file to include the following line in the dependencies
section:
"com.unity.nuget.newtonsoft-json": "3.2.1"
var saveLoadService = new JsonSaveLoadService();
// Saving a simple data type :D
int highScore = 100;
saveLoadService.Save(highScore, "highScore");
// Saving a custom object :D
var playerData = new PlayerData { Name = "Rimuru", Level = 10 };
saveLoadService.Save(playerData, "playerData");
// Saving a collection :D
var scores = new List<int> { 100, 200, 300 };
saveLoadService.Save(scores, "scores");
var loadedHighScore = saveLoadService.Load("highScore", 0);
var loadedPlayerData = saveLoadService.Load("playerData", new PlayerData());
var loadedScores = saveLoadService.Load("scores", new List<int>());
-
Dictionary: Save and load
Dictionary<TKey, TValue>
collections.var monsterConfigs = new Dictionary<int, MonsterConfig> { { 1, new MonsterConfig { /* Initialization */ } }, }; saveLoadService.Save(monsterConfigs, "monsterConfigs"); var loadedMonsterConfigs = saveLoadService.Load("monsterConfigs", new Dictionary<int, MonsterConfig>());
-
HashSet: Save and load
HashSet<T>
collections.var uniqueItems = new HashSet<string> { "Sword", "Shield" }; saveLoadService.Save(uniqueItems, "uniqueItems"); var loadedUniqueItems = saveLoadService.Load("uniqueItems", new HashSet<string>());
-
List: Save and load
List<T>
collections.var itemList = new List<string> { "Apple", "Banana" }; saveLoadService.Save(itemList, "itemList"); var loadedItemList = saveLoadService.Load("itemList", new List<string>());
-
Queue: Save and load
Queue<T>
collections.var messageQueue = new Queue<string>(); messageQueue.Enqueue("Hello"); messageQueue.Enqueue("World"); saveLoadService.Save(messageQueue, "messageQueue"); var loadedMessageQueue = saveLoadService.Load("messageQueue", new Queue<string>());
-
Stack: Save and load
Stack<T>
collections.var undoCommands = new Stack<string>(); undoCommands.Push("Command1"); undoCommands.Push("Command2"); saveLoadService.Save(undoCommands, "undoCommands"); var loadedUndoCommands = saveLoadService.Load("undoCommands", new Stack<string>());
For ease of debugging and testing in the Unity Editor, JsonSaveLoadService
allows specifying a custom save path:
#if UNITY_EDITOR
JsonSaveLoadService.SetCustomPathInEditor("Assets/Saves");
#endif