Skip to content
This repository has been archived by the owner on Feb 10, 2024. It is now read-only.

lordcodes/unity-powerprefs

Repository files navigation

PowerPrefs

Latest release Twitter: @lordcodes Twitter: @lordcodes

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.

 

FeaturesInstallUsage


Features

▶︎ Many extra types

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.

▶︎ Easy-to-use

You can be up and running without any configuration, simply start getting and setting values.

▶︎ Type-safe accessors

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.

▶︎ Migration

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.

▶︎ Open for extension

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!

▶︎ Fully documented

The public API is fully documented, including code documentation.

Install

PowerPrefs can be installed using the Unity Package Manager.

  1. Open the Package Manager within Unity.
  2. Select to add via Git URL.
  3. Enter the Git URL.
  4. 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.

Usage

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.

Advanced Usage

Re-use Accessor

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");

Contributing or Help

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.

About

Read and write values of various types to Unity PlayerPrefs with way more options than before!

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published