WebPrefs is a package which serves as a replacement to Unity's PlayerPrefs saving system, made to integrate much better into WebGL games. WebPrefs makes your save data harder to lose after publishing new game builds, and gives you much more power and flexibility with the data you are saving.
By default, PlayerPrefs simply store data inside the IndexedDB, but this is not
great because we lose access to this data if we ever make a new build of the game. Instead this
gets around that by using a JavaScript bridge to save to the browser's localStorage
instead, along with keeping backups and fallbacks inside the IndexedDB, so your
data is super hard to lose!
Even though this was made for WebGL games, simply calling WebPrefs to save your data will also properly channel it through PlayerPrefs, using the custom serialization to allow for all of the more advanced data types this lets you save, so don't worry about any PC port headaches for your web game.
https://github.com/obradev/WebPrefs.git
It should be installed now. You can head over to Samples for an optional demo scene.
Add the using directive to the top of your script:
using ObraDev.WebPrefs;
After this you can instantly start using the package to save data.
WebPrefs.Save(string key, object value);
key is the name you will know the data by (e.g. "coins"),
and value can be various types: string, int,
float, bool, etc.
WebPrefs.Save("coins", 100);
There are two ways to load data:
int coins = WebPrefs.Load<int>("coins");
// With a default fallback:
int coins = WebPrefs.Load<int>("coins", 0);
int coins = WebPrefs.LoadInt("coins");
WebPrefs.HasKey(string key); // returns bool
WebPrefs.DeleteKey(string key); // clears one key
WebPrefs.ClearAllData(); // clears everything
WebPrefs.Reload(); // reloads from IndexedDB backup (or saves via PlayerPrefs on non-web)
WebPrefs.GetRawData(); // returns the full serialized localStorage string (useful for debugging)
WebPrefs.GetAllKeys(); // returns a string array of all currently saved keys
WebPrefs.GetKeyType(string key); // returns the Type a key was saved as, or null if not found
Subscribe to these events to react to internal WebPrefs actions from anywhere in your code:
WebPrefs.OnKeySaved += (string key) => { }; // fires after any successful save
WebPrefs.OnKeyDeleted += (string key) => { }; // fires after a key is deleted
WebPrefs.OnDataCleared += () => { }; // fires after ClearAllData()
WebPrefs.OnDataRestored += () => { }; // fires when localStorage is recovered from IDB backup
WebPrefs.OnSaveFailed += () => { }; // fires when localStorage write fails
WebPrefs.OnStorageFull += () => { }; // fires when the 10KB localStorage cap is reached
Example usage:
void Start()
{
WebPrefs.OnKeySaved += key => Debug.Log("Saved: " + key);
WebPrefs.OnDataRestored += () => ShowRestoredMessage();
}
intfloatstringboolVector2Vector3Vector4QuaternionColorColor32SerializableTransform — a custom type that holds Position, Rotation, and Scale
under one key. Instantiate with new SerializableTransform(transform) or by
assigning the three fields manually.