JsonSaveSystem is a save system for Unity that uses the System.Text.Json namespace provided by .NET. It is designed to be versatile, easy to use, and easy to maintain. It has built-in safety code to ensure correct utilization of the library.
- Each component(script) has its own GUID. Each component is serialized separately.
- One gameobject can have multiple serialized components. These components can be of the same type.
- The system saves objects by scene, meaning each scene can be loaded and unloaded individually. Supports multi-scene setup.
- The data structure saves all scenes into one file, making it simple to transfer and to keep track of save file.
- Data is serialized to Utf8 bytes, which allows encryption and modification before writing to file.
- System.Text.Json is a fully featured Json Serializer and Deserializer. It supports most data structures and fields, in contrast to Unity's JsonUtility which cannot handle those types.
- It offers excellent performance compared to other C# Json Serializers, such as Json.NET and LitJson, only losing to JsonUtility.
- It is built by Microsoft, hence is guaranteed to be stable.
- Attach SaveManager component to a persistent gameobject.
- Create a script that handles saving by interacting with SaveManager's API.
- Have the component that needs to be serialized inherit from SaveableMono instead of MonoBehaviour.
- Implement saving and loading logic for that component.
- See Examples folder for more info.
- Compared to Json.NET (10k iterations):
- ~2.13x faster serialization(120ms vs 254ms)
- ~1.27x faster deserialization than Json.NET (175ms vs 223ms)
- ~51% less GC allocation during serialization (9.5MB vs 19.3MB)
- ~70% less GC allocation during deserialization (9.4MB vs 31.6MB)
- Compared to JsonUtility (10k iterations):
- ~2x slower serialization (120ms vs 58ms)
- ~1.1x slower deserialization (175ms vs 153ms)
- ~3.1x more GC allocation during serialization (9.5MB vs 3MB)
- ~2.4x more GC allocation during deserialization (9.4MB vs 3.9MB)
- Due to technical limitations of the Unity Engine, source generation mode is not available, and reflection mode is used. Once Unity Engine updates their runtime to .NET 6, source generation mode will be supported, and performance should be on par with JsonUtility.
Planned features:
- Support for gameobjects that are dynamically constructed at runtime
- Scene mapping that allows spitting save file into multiple smaller files
Known bugs:
- none