Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rimurudev/unity-jsonsaveloadservice

šŸˆ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 fo
https://github.com/rimurudev/unity-jsonsaveloadservice

json json-data local-save newtonsoft-json rimuru-dev rimurudev save save-load save-load-model save-system serialization simple-json unity-json unity-package unity-save

Last synced: 6 days ago
JSON representation

šŸˆ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 fo

Awesome Lists containing this project

README

        

šŸˆ Json Save Load Service šŸˆ




Made With Unity


License


Last Commit


Repo Size


Downloads


Last Release


GitHub stars


GitHub user stars




## Overview
`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.

## Dependencies
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`.

## Installing Newtonsoft.Json in Unity
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:

1. Open your Unity project.
2. Navigate to `Window` > `Package Manager`.
3. Click the `+` button in the top left corner of the Package Manager window.
4. Select `Add package from git URL...`.
5. Enter `[email protected]` and click `Add`.
6. 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:

```json
"com.unity.nuget.newtonsoft-json": "3.2.1"
```

## Usage

### Saving Data
```csharp
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 { 100, 200, 300 };
saveLoadService.Save(scores, "scores");
```

### Loading Data
```csharp
var loadedHighScore = saveLoadService.Load("highScore", 0);
var loadedPlayerData = saveLoadService.Load("playerData", new PlayerData());
var loadedScores = saveLoadService.Load("scores", new List());
```

### Working with Collections
- **Dictionary**: Save and load `Dictionary` collections.
```csharp
var monsterConfigs = new Dictionary
{
{ 1, new MonsterConfig { /* Initialization */ } },
};
saveLoadService.Save(monsterConfigs, "monsterConfigs");

var loadedMonsterConfigs = saveLoadService.Load("monsterConfigs", new Dictionary());
```

- **HashSet**: Save and load `HashSet` collections.
```csharp
var uniqueItems = new HashSet { "Sword", "Shield" };
saveLoadService.Save(uniqueItems, "uniqueItems");

var loadedUniqueItems = saveLoadService.Load("uniqueItems", new HashSet());
```

- **List**: Save and load `List` collections.
```csharp
var itemList = new List { "Apple", "Banana" };
saveLoadService.Save(itemList, "itemList");

var loadedItemList = saveLoadService.Load("itemList", new List());
```

- **Queue**: Save and load `Queue` collections.
```csharp
var messageQueue = new Queue();
messageQueue.Enqueue("Hello");
messageQueue.Enqueue("World");
saveLoadService.Save(messageQueue, "messageQueue");

var loadedMessageQueue = saveLoadService.Load("messageQueue", new Queue());
```

- **Stack**: Save and load `Stack` collections.
```csharp
var undoCommands = new Stack();
undoCommands.Push("Command1");
undoCommands.Push("Command2");
saveLoadService.Save(undoCommands, "undoCommands");

var loadedUndoCommands = saveLoadService.Load("undoCommands", new Stack());
```

## Editor-Only Custom Path
For ease of debugging and testing in the Unity Editor, `JsonSaveLoadService` allows specifying a custom save path:

```csharp
#if UNITY_EDITOR
JsonSaveLoadService.SetCustomPathInEditor("Assets/Saves");
#endif
```