A powerful and extensible preferences package for Unity, built on top of PlayerPrefs
. PowerPrefs provides access to many more types than before, whilst also providing type-safe accessors to your key-value store.
Features • Installation • Usage
Read and write values to PlayerPrefs
with support for many extra types, such as bool
, char
, DateTime
and long
. There is also the possibility for adding many more in the future.
You can be up and running without any configuration, simply start getting and setting values.
Values are retrieved and written through an accessor class which is typed. This means there are no types in the method names and you can even store the accessor class to read and write a key safely, without mentioning the type again. You can also store an accessor for a particular key to be used across your codebase without re-specifying the key.
A method is provided to migrate a value from one key to another, which is useful if you wish to rename one of your keys safely.
The classes which read and write values contain instance methods, allowing you to add extra features through extension methods if you wish. Even better, contribute them back to the library for others to use too!
The public API is fully documented, including code documentation.
It is developed and tested on Unity 2018.1.
You can download the latest version of PowerPrefs by using this link.
Once you have the unitypackage
file, you can import it into your Unity project. If your project is already open then you can simply double-click the downloaded package. Alternatively, go to Assets -> Import Package -> Custom Package
within the Unity editor.
First retrieve a typed accessor and then get or set a value:
PowerPrefs.ForBool().Get("aBoolean");
PowerPrefs.ForInt().Get("anInteger", -1);
PowerPrefs.ForLong().Set("aLong", 123456);
PowerPrefs.ForString().Set("aString", "Hello");
For the Get
call you can provide a default value to return if the key doesn't exist. If this isn't provided then the default for that type will be used instead. E.g. for int
it would be 0.
You have the option of storing and re-using the accessor class.
var accessor = PowerPrefs.ForInt();
...
var myValue = accessor.Get("myKey");
accessor.Set("meyKey", 5);
An accessor for a particular key is also available: PowerPrefsKeyAccessor<T>
.
var keyAccessor = PowerPrefs.ForString().KeyAccessor("someKey");
...
var myValue = keyAccessor.Get();
keyAccessor.Set("newValue");
If you find something that you don't think is working correctly, you have a feature you would like to see in PowerPrefs or just because you want to ask for some help, please open an issue.
If you would like to contribute some changes to PowerPrefs, I would greatly appreciate a PR. If you would like to make a major change, please create an issue first to discuss it.
Andrew Lord @andrewlord1990