SEO marketer with an experience of 7+ years.
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.
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!
PowerPrefs provides access to many more data types and also opens up the possibility for even more features as well.
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.
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.
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.
Instead of getting the accessor each time, you can also store it for reuse.
This mechanism means the user doesn’t need to specify the type in every call to the 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.
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.
Thanks for reading, I welcome any comments or suggestions you might have.