Skip to content

🐈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

License

Notifications You must be signed in to change notification settings

RimuruDev/Unity-JsonSaveLoadService

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🐈 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 com.unity.nuget.newtonsoft-json@3.2.1 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:

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

Usage

Saving Data

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");

Loading Data

var loadedHighScore = saveLoadService.Load("highScore", 0);
var loadedPlayerData = saveLoadService.Load("playerData", new PlayerData());
var loadedScores = saveLoadService.Load("scores", new List<int>());

Working with Collections

  • 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>());

Editor-Only Custom Path

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

About

🐈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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages