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.1and 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"varsaveLoadService=newJsonSaveLoadService();// Saving a simple data type :DinthighScore=100;saveLoadService.Save(highScore,"highScore");// Saving a custom object :DvarplayerData=newPlayerData{Name="Rimuru",Level=10};saveLoadService.Save(playerData,"playerData");// Saving a collection :Dvarscores=newList<int>{100,200,300};saveLoadService.Save(scores,"scores");varloadedHighScore=saveLoadService.Load("highScore",0);varloadedPlayerData=saveLoadService.Load("playerData",newPlayerData());varloadedScores=saveLoadService.Load("scores",newList<int>());Dictionary: Save and load
Dictionary<TKey, TValue>collections.varmonsterConfigs=newDictionary<int,MonsterConfig>{{1,newMonsterConfig{/* Initialization */}},};saveLoadService.Save(monsterConfigs,"monsterConfigs");varloadedMonsterConfigs=saveLoadService.Load("monsterConfigs",newDictionary<int,MonsterConfig>());
HashSet: Save and load
HashSet<T>collections.varuniqueItems=newHashSet<string>{"Sword","Shield"};saveLoadService.Save(uniqueItems,"uniqueItems");varloadedUniqueItems=saveLoadService.Load("uniqueItems",newHashSet<string>());
List: Save and load
List<T>collections.varitemList=newList<string>{"Apple","Banana"};saveLoadService.Save(itemList,"itemList");varloadedItemList=saveLoadService.Load("itemList",newList<string>());
Queue: Save and load
Queue<T>collections.varmessageQueue=newQueue<string>();messageQueue.Enqueue("Hello");messageQueue.Enqueue("World");saveLoadService.Save(messageQueue,"messageQueue");varloadedMessageQueue=saveLoadService.Load("messageQueue",newQueue<string>());
Stack: Save and load
Stack<T>collections.varundoCommands=newStack<string>();undoCommands.Push("Command1");undoCommands.Push("Command2");saveLoadService.Save(undoCommands,"undoCommands");varloadedUndoCommands=saveLoadService.Load("undoCommands",newStack<string>());
For ease of debugging and testing in the Unity Editor, JsonSaveLoadService allows specifying a custom save path:
#if UNITY_EDITORJsonSaveLoadService.SetCustomPathInEditor("Assets/Saves");
#endif