Easy save system for Unity, based on Newtonsoft Json Package and Newtonsoft.Json-for-Unity.Converters for serialization.
- Сan save almost any type (List, Class, Struct, most Unity types, etc)
- Can save asset references
- Сan add default data (if the file does not contain a value for the key, the data is taken from the default data)
- Simple data encryption
- Easy to use
- Json Constructor in Unity Editor (edit the save file directly in the editor)
- Flexible (use an existing component, or create your own save system based on the SFile class)
The package is currently in an early stage of development, so some methods and concepts may change over time. Use at your own risk
-
Open the
manifest.jsonin your project by path[Project Folder]\Packages\manifest.jsonand add Scoped Registry:"scopedRegistries": [ { "name": "Packages from jillejr", "url": "https://npm.cloudsmith.io/jillejr/newtonsoft-json-for-unity/", "scopes": [ "jillejr" ] } ],
Add this before the dependencies block.
This item is required to install a dependency that fixes an error when serializing Unity objects such as Vector3, Quaternion, Color, and many more.
-
Open the Package Manager and "Add package from git URL...". Paste
https://github.com/TarasK8/Save-System-for-Unity.gitand Add.
- Create Empty GameObject and add
SaveSystemObjectcomponent. - Extend the MonoBehaviour class for saving with the
ISaveableinterface and implement itpublic void OnSave(SFile file) { } public void OnLoad(SFile file) { }
If necessary, you can implement only one of these methods
- In the implemented methods, write the logic for saving and loading, for example:
private int _valueForSave; public void OnSave(SFile file) { file.Write("Key", _valueForSave); } public void OnLoad(SFile file) { _valueForSave = file.Read<int>("Key"); }
- After that, depending on the
SaveSystemObjectobject settings, it will save allISaveableobjects. Or you can save the data manually, using theSceneSave.Save()method.
- You can also make a custom save system by directly using the SFile class, for example:
string path = System.IO.Path.Combine(Application.persistentDataPath, "Saves/save.json"); SFile file = new SFile(path); int valueToSave = 1234; file.Write("Key", valueToSave); file.Save();