Data - VirtueSky/sunflower GitHub Wiki

Introduction

flowchart LR
    Unity.Serialization{Unity.Serialization} --> W(Write) & R(Read) 
    click Unity.Serialization "https://docs.unity3d.com/Packages/[email protected]/manual/index.html" _blank
    Data[("Data <br> Profile 0<br>.<br>.<br>.<br> Profile N <br> Dictionary&lt;string,byte[]>")] --> |Default use profile 0| ChangeProfile(ChangeProfile) 
    Data --> |Auto save on Application pause or quit| SaveToFile(SaveToFile)--> Unity.Serialization
    Data --> |Auto load when startup| LoadFromFile(LoadFromFile)--> Unity.Serialization
    Data --> Get(Get&lt;T>) & Set(Set&lt;T>) & DeleteKey(DeleteKey) & HasKey(HasKey) & DeleteAll(DeleteAll)
    File{{File Data}} --- W & R
Loading
  • Use Newtonsoft.Json to serialize data (version < 2.4.5)
  • Starting from version 2.4.6, the data system changes, switch to using Unity.Serialization to serialize binary data
  • Data allows you to store data in byte[] form in blocks called profiles. Each profile is a Dictionary with the key as a string and the value as a byte[]. The default profile will be 0 if you want to Load or Save in another profile you will need to call the ChangeProfile function.

Note: Please consider carefully before updating to the new version (may cause loss of previous data)

Use

  • Initialize data when loading the game: GameData.Init()
  • Change Profile: GameData.ChangeProfile(int profile)
  • Load Data: GameData.Load() Load all data from file for game, data will be loaded automatically when starting the game
  • Get Data: GameData.Get("KEY", valueDefault); use similar to PlayerPrefs
  • Set Data: GameData.Set("KEY", value); use similar to PlayerPrefs
  • Save Data: GameData.Save() data will be automatically saved when quit game or pause game.
  • Has Key: GameData.HasKey(string key) to check if the profile has a key,
  • DeleteKey: GameData.DeleteKey(string key)to delete the key from the profile
  • DeleteAll: GameData.DeleteAll() to delete the entire key
  • Backup: GameData.Backup() Get raw byte[] of all data of profile
  • Restore: GameData.Restore(byte[] bytes) Load from byte[]

Comparison: DataStorage (Sunflower) vs PlayerPrefs in Unity

The table below summarizes the main pros and cons between DataStorage (Sunflower Framework) and PlayerPrefs in Unity.

Criteria DataStorage (Sunflower) PlayerPrefs
Structure & Data Types Supports multiple data types (bool, int, float, string, object...), with an auto-save mechanism. Only supports int, float, and string. Does not support object or vector types.
Extensibility Can be extended via serialization/binary files, easier migration and versioning. Limited extensibility, no versioning or advanced save structure.
Security & Stability Saves data to separate files that can be encrypted or have controlled paths – safer than PlayerPrefs. Stores data in registry/plist/xml, easily editable by users.
Ease of Use Requires initial setup, more complex coding. Extremely simple, easy-to-use API.
Recommended Use Case Suitable for games with complex data systems, requiring synchronization across modules (Ads, IAP, Remote Config...). Best for small data like settings, flags, or current level tracking.

Conclusion

  • PlayerPrefs: Ideal for prototypes or small data, fast and simple to implement.
  • DataStorage (Sunflower): Recommended for larger projects that require a clear data architecture, better maintainability, and scalability.

Benchmark Results: Data Systems Comparison

Environment

  • Unity 6000.0.58f2
  • IL2CPP
  • SamSung A04/ Ram 3GB/32GB

Setup test

  • Perform read/write/read/erase 1000 times.

The table below presents the benchmark results comparing PlayerPrefs and GameData (Sunflower DataStorage) performance.

Unnamed: 0 PlayerPrefs Avg GameData Avg.1
Count 1 2 3 4 5 1 2 3 4 5
Save when value changed
Write time (ms) 27 32 25 25 28 27,4 84 99 89 88 96 91,2
Read time (ms) 25 37 27 33 41 32,6 19 21 16 18 23 19,4
Delete time (ms) 36 26 39 36 33 34 32 17 22 11 21 20,6
Save when pause app
Write time (ms) 25 27 24 27 29 26,4 37 28 26 23 31 29
Read time (ms) 25 31 35 25 37 30,6 20 24 27 27 26 24,8
Delete time (ms) 29 39 30 39 26 32,6 16 14 13 22 17 16,4

Project benchmark here

⚠️ **GitHub.com Fallback** ⚠️