PowerPrefs — Unity PlayerPrefs Just Got A Whole Lot More Powerful

Written by lordcodes | Published 2017/11/02
Tech Story Tags: unity3d | csharp | unity

TLDRvia the TL;DR App

Almost all Unity developers will very quickly have discovered PlayerPrefs. It is a simple way to store and retrieve values for use within your project. It is well known that as it isn’t encrypted you shouldn’t store anything sensitive in there. However, it can be very useful for storing other values, such as user preferences or configuration.

Unfortunately, the PlayerPrefs API has some issues, which make it less powerful than I would have liked and not easily extensible.

  1. Its methods are all static.
  2. Only string, int and float values are supported.
  3. There are separate getters and settings for the different types. This means you need to specify type of a particular value when getting it and setting it.

One of the first times I found myself wanting to use PlayerPrefs was to store whether the user had muted the music through the in-game settings. This lead me to discover that there was no support for boolean values. It was very simple to write a couple of functions to do this using GetInt and SetInt. However, due to the methods all being static you are unable to make these discoverable through extension methods.

Point 3 above might not seem like a large issue, however, it does reduce clarity in the API and can easily lead to bugs. The user of the API needs to remember the data type a particular value is stored with when they retrieve it. Instead, the key could easily be linked with a particular type and then enforced through the API. This would make things clearer and result in the user of the API not needing to represent this information themselves.

For a while I lived with these issues, but when I found myself wanting to add even more data types to PlayerPrefs it became clear that I needed a better solution.

Please welcome PowerPrefs!

andrewlord1990/unity-powerprefs

PowerPrefs provides access to many more data types and also opens up the possibility for even more features as well.

What does it do?

  • You can read and write values of many extra types, such as bool, char, DateTime and long.
  • Values are stored and retrieved using a typed accessor class. This means that a key is linked to a particular data type and enforced through the API. Separate methods are no longer required in the API for the different types.
  • An accessor for a particular key is available, which can be stored and then used throughout your codebase to interact with that key.
  • The ability to safely migrate a value from one key to another, which can be useful if you ever need to rename one of your keys.
  • The API is no longer static, allowing you to add features through extension methods. Even better, you can contribute them back to the library for others to use as well.

How do I get it?

The source code is available on GitHub, where you can download PowerPrefs from the releases page.

The file you want to download is the unitypackage file. You would then import this 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.

How do I use it?

Basic usage

The most basic usage would be using it in the same way as the existing PlayerPrefs API. You simply retrieve a typed accessor and then get or set a value.

<a href="https://medium.com/media/796a411c8420dae00d544c4caf3419ff/href">https://medium.com/media/796a411c8420dae00d544c4caf3419ff/href</a>

You can in the example, that you can provide a default value to return if the key doesn’t exist. However, if you don’t provide one then the default for the type will used, such as 0 for int values.

Re-use Accessor

Instead of getting the accessor each time, you can also store it for reuse.

<a href="https://medium.com/media/c0ce9cfab79a717cd61e2a00331101b4/href">https://medium.com/media/c0ce9cfab79a717cd61e2a00331101b4/href</a>

This mechanism means the user doesn’t need to specify the type in every call to the accessor.

Key Accessor

As meaning at the beginning, one of the problems with the PlayerPrefs API is that the user needs to know the type for a particular key when storing and retrieving it. The answer to this is using accessors for a particular key, which are also typed.

<a href="https://medium.com/media/11c498326522059f9f8892d46ac30269/href">https://medium.com/media/11c498326522059f9f8892d46ac30269/href</a>

Now, rather than storing your keys as constants, you can simply store the key accessors. This allows you to easily store and retrieve the value without needing to remember its type each time.

PowerPrefs is more powerful than the original PlayerPrefs API, it doesn’t add much code to your project, is simple to use and is extensible. If you agree, then please try it out today! Even better, contribute to the library and help me make it even better.

andrewlord1990/unity-powerprefs

Thanks for reading, I welcome any comments or suggestions you might have.


Published by HackerNoon on 2017/11/02