Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

10 Commits

Repository files navigation

🐈 Json Save Load Service 🐈

Made With UnityLicenseLast CommitRepo SizeDownloadsLast ReleaseGitHub starsGitHub 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

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

Loading Data

varloadedHighScore=saveLoadService.Load("highScore",0);varloadedPlayerData=saveLoadService.Load("playerData",newPlayerData());varloadedScores=saveLoadService.Load("scores",newList<int>());

Working with Collections

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

Editor-Only Custom Path

For ease of debugging and testing in the Unity Editor, JsonSaveLoadService allows specifying a custom save path:

#if UNITY_EDITORJsonSaveLoadService.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

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages