|
1 | | -# Unity-JsonSaveLoadService |
| 1 | +# JsonSaveLoadService |
| 2 | + |
| 3 | +## Overview |
| 4 | +`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. |
| 5 | + |
| 6 | +## Usage |
| 7 | + |
| 8 | +### Saving Data |
| 9 | +```csharp |
| 10 | +var saveLoadService = new JsonSaveLoadService(); |
| 11 | + |
| 12 | +// Saving a simple data type :D |
| 13 | +int highScore = 100; |
| 14 | +saveLoadService.Save(highScore, "highScore"); |
| 15 | + |
| 16 | +// Saving a custom object :D |
| 17 | +var playerData = new PlayerData { Name = "Rimuru", Level = 10 }; |
| 18 | +saveLoadService.Save(playerData, "playerData"); |
| 19 | + |
| 20 | +// Saving a collection :D |
| 21 | +var scores = new List<int> { 100, 200, 300 }; |
| 22 | +saveLoadService.Save(scores, "scores"); |
| 23 | +``` |
| 24 | + |
| 25 | +### Loading Data |
| 26 | +```csharp |
| 27 | +var loadedHighScore = saveLoadService.Load("highScore", 0); |
| 28 | +var loadedPlayerData = saveLoadService.Load("playerData", new PlayerData()); |
| 29 | +var loadedScores = saveLoadService.Load("scores", new List<int>()); |
| 30 | +``` |
| 31 | + |
| 32 | +### Working with Collections |
| 33 | +- **Dictionary**: Save and load `Dictionary<TKey, TValue>` collections. |
| 34 | + ```csharp |
| 35 | + var monsterConfigs = new Dictionary<int, MonsterConfig> |
| 36 | + { |
| 37 | + { 1, new MonsterConfig { /* Initialization */ } }, |
| 38 | + }; |
| 39 | + saveLoadService.Save(monsterConfigs, "monsterConfigs"); |
| 40 | + |
| 41 | + var loadedMonsterConfigs = saveLoadService.Load("monsterConfigs", new Dictionary<int, MonsterConfig>()); |
| 42 | + ``` |
| 43 | + |
| 44 | +- **HashSet**: Save and load `HashSet<T>` collections. |
| 45 | + ```csharp |
| 46 | + var uniqueItems = new HashSet<string> { "Sword", "Shield" }; |
| 47 | + saveLoadService.Save(uniqueItems, "uniqueItems"); |
| 48 | + |
| 49 | + var loadedUniqueItems = saveLoadService.Load("uniqueItems", new HashSet<string>()); |
| 50 | + ``` |
| 51 | + |
| 52 | +- **List**: Save and load `List<T>` collections. |
| 53 | + ```csharp |
| 54 | + var itemList = new List<string> { "Apple", "Banana" }; |
| 55 | + saveLoadService.Save(itemList, "itemList"); |
| 56 | + |
| 57 | + var loadedItemList = saveLoadService.Load("itemList", new List<string>()); |
| 58 | + ``` |
| 59 | + |
| 60 | +- **Queue**: Save and load `Queue<T>` collections. |
| 61 | + ```csharp |
| 62 | + var messageQueue = new Queue<string>(); |
| 63 | + messageQueue.Enqueue("Hello"); |
| 64 | + messageQueue.Enqueue("World"); |
| 65 | + saveLoadService.Save(messageQueue, "messageQueue"); |
| 66 | + |
| 67 | + var loadedMessageQueue = saveLoadService.Load("messageQueue", new Queue<string>()); |
| 68 | + ``` |
| 69 | + |
| 70 | +- **Stack**: Save and load `Stack<T>` collections. |
| 71 | + ```csharp |
| 72 | + var undoCommands = new Stack<string>(); |
| 73 | + undoCommands.Push("Command1"); |
| 74 | + undoCommands.Push("Command2"); |
| 75 | + saveLoadService.Save(undoCommands, "undoCommands"); |
| 76 | + |
| 77 | + var loadedUndoCommands = saveLoadService.Load("undoCommands", new Stack<string>()); |
| 78 | + ``` |
| 79 | + |
| 80 | +## Editor-Only Custom Path |
| 81 | +For ease of debugging and testing in the Unity Editor, `JsonSaveLoadService` allows specifying a custom save path: |
| 82 | + |
| 83 | +```csharp |
| 84 | +#if UNITY_EDITOR |
| 85 | +JsonSaveLoadService.SetCustomPathInEditor("Assets/Saves"); |
| 86 | +#endif |
| 87 | +``` |
0 commit comments