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.
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.
PowerPrefs can be installed using the Unity Package Manager.
- Open the Package Manager within Unity.
- Select to add via Git URL.
- Enter the Git URL.
- The package can now be used within the Unity project.
The Git URL for the release branch:
https://github.com/lordcodes/unity-powerprefs.git#upm
The Git URL for a specific version:
https://github.com/lordcodes/unity-powerprefs.git#v{VERSION}
If a previous version of PowerPrefs had been imported using a unitypackage
, make sure to remove it from the project before importing via Unity Package Manager.
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 notice any bugs or have a new feature to suggest, please check out the contributing guide. If you want to make changes, please make sure to discuss anything big before putting in the effort of creating the PR.
To reach out, please contact @lordcodes on Twitter.