Introduction
Player Prefs Lite is a drop in replacement for the Unity Player Prefs system. Whilst it is 100% compatible with Unity's system it greatly increases the data types that can be used.
Please refer to the section Quick Start to quickly upgrade an existing script from Unity PlayerPrefs to PlayerPrefs Lite.
PlayerPrefs Lite was developed using the technology from ScriptableObject Pro and is designed to fulfil one specifc function. This product is designed to handle small amounts of simple data quckly and efficiently if your data is large or more complex then ScriptableObject Pro is likely to be a better fit for your project.
This version of the utility was written in C# with Unity .Net 4.7.1 libraries and tested using Unity 2020.3 and 2021.3 for Standalone, WebGL, Android and IOS but should function on all platforms supported by Unity.
Whilst every effort has been made to ensure the compatibility of PlayerPrefs Lite with all Unity versions, the correct functioning of the software with Unity Alpha, Beta or Technical releases cannot be guaranteed.
The code that makes up this utility is copyright SteveSmith.SoftWare 2022. Unless specifically stated in the code file or this documentation, no part of the system may be copied or modified. Neither may it be stored in such a way that would make it available to the public outside of the Unity Asset Store.
This documentation is available online at https://stevesmith.software. You will also find there the form for filing a bug report should it be necessary.
Steve Smith 2022.
<- Previous
Next ->
Quick Start
Unity Player Prefs
using UnityEngine;
public class MyClass : MonoBehaviour
{
public int points = 0;
void Start ()
{
points = PlayerPrefs.GetInt("points", 0);
}
void OnTriggerEnter(Collider other)
{
points ++;
PlayerPrefs.SetInt("points", points);
}
}
Player Prefs Lite
using UnityEngine;
using PlayerPrefs = SSSoftware.PlayerPrefsLite.PlayerPrefs;
public class MyClass : MonoBehaviour
{
public int points = 0;
void Start ()
{
points = PlayerPrefs.GetInt("points", 0);
}
void OnTriggerEnter(Collider other)
{
points ++;
PlayerPrefs.SetInt("points", points);
}
}
Add a using statement
using PlayerPrefs = SSSoftware.PlayerPrefsLite.PlayerPrefs;
Your code will now use the Player Prefs Lite system.
No further code changes are required
<- Previous
Next ->
PlayerPrefs
The Player Prefs Lite PlayerPrefs class is a static class that is ready to use once the following line has been added to a script
using PlayerPrefs = SSSoftware.PlayerPrefsLite.PlayerPrefs;
Your code will now use the Player Prefs Lite system and the full range of Player Prefs Lite data types will be available to use exactly as if the original player prefs was still in use.
Overview
The Player Prefs Lite system is driven by 2 backing files.
The first is a Unity ScriptableObject which can be found in the PlayerPrefsLite/Resources folder and the contents can be viewed via the Inspector.
Note: Should this file be deleted a new one can be created via the Asset->Create menu.
The second is a ScriptableObjectPro file which is created at runtime to store preference changes, this file will persist across builds in the Unity Editor and at runtime but is NOT included in the build of your game. This means that preference values set in the Unity Editor will NOT be in a build.
If you require data to be carried over from the Unity Editor into a build then please refer to ScriptableObject Pro.
<- Previous
Next ->
Methods
The following are the public methods which can be called by user code to control the functioning of Player Prefs Lite.
AutoSave
|
void |
AutoSave(bool save, float interval) |
|
|
Description |
|
By default the preference values will be saved automatically when the application quits or looses focus. A call to AutoSave(false) will disable this behaviour and it is then the responsibility of user code to call Save to save the preferences.
Calling AutoSave(true) will restore the default behaviour and calling AutoSave(true,30f) will start a timed save.
|
|
|
Parameters |
|
bool save |
true - turn autosave on. false - turn autosave off |
|
float interval |
Automatically save the preference values. Interval is a number of seconds to wait between saves |
|
|
Return values |
|
none |
|
|
|
|
Example |
|
PlayerPrefs.AutoSave(false); // Disable the default save behaviour
PlayerPrefs.AutoSave(true); // Enable the default save behaviour
PlayerPrefs.AutoSave(true, 30f); // Enable the default save behaviour and save the preferences every 30 seconds
|
<- Previous
Next ->
Delete
|
void |
Delete(string key) |
|
|
Description |
|
Delete a preference |
|
|
Parameters |
|
string key |
The name of the preference to delete |
|
|
Return values |
|
none |
|
|
|
|
Example |
|
PlayerPrefs.Delete("myPref"); // Delete the 'myPref' preference
|
<- Previous
Next ->
DeleteAll
|
void |
DeleteAll() |
|
|
Description |
|
Delete all of the preference values |
|
|
Parameters |
|
none |
|
|
|
Return values |
|
none |
|
|
|
|
Example |
|
PlayerPrefs.DeleteAll(); // Deletes all preference values
|
<- Previous
Next ->
Get
|
T |
Get<T>(string key, T defaultValue) |
|
|
Description |
|
Returns the preference value specified by key if that key exists. If the key does not exist it returns the defaultValue either supplied or for that data type. |
|
|
Parameters |
|
string key |
The name of the preference to retrieve |
|
T defaultValue |
Optional. The value to return if no preference is found. If no defalut is supplied then the default value for the type is returned where T is any of the supported data types |
|
|
Return values |
|
T |
The preference value or default value as Type T where T is any of the supported data types |
|
|
|
Example |
|
int score = PlayerPrefs.Get<int>("score");
int damage = PlayerPrefs.Get("damage",10);
Returns the preference value for 'score' or 'damage' if they exist.
if 'score' has no value then 0 (the default value for type int) is returned
if 'damage' has no value then 10 will be returned.
Note: Because no default value has been supplied for 'score' we must specify the return type using <int>
Each of the data types has a corresponding specific Get method to avoid this
int score = PlayerPrefs.Get<int>("score");
int score = PlayerPrefs.GetInt("score");
These 2 calls are equivalent
|
|
bool |
Get<T>(string key, out T value, T defaultValue) |
|
|
Description |
|
This Get method returns the preference or default value into the variable specified by the out parameter and reports whether the preference or the default has been used |
|
|
Parameters |
|
string key |
the name of the preference to retrieve |
|
out T value |
The variable in which to return the value where T is any of the supported data types |
|
T defaultValue |
Optional. The default value to return if no preference is found. If no default value is supplied the default value for Type T is used where T is any of the supported data types |
|
|
Return values |
|
true |
The value in the out variable is a preference value |
|
|
false |
The value in the out variable is the default value |
|
|
|
Example |
|
int score=0;
if (PlayerPrefs.Get("score", out score))
{
// The 'score' preference was found
// The variable score contains the value
}
else
{
// The 'score' preference was NOT found
// The variable score contains 0
}
|
<- Previous
Next ->
HasKey
|
bool |
HasKey(string key) |
|
|
Description |
|
Checks if a preference with the name specified by key exists in the preferences |
|
|
Parameters |
|
string key |
The name of the preference to check |
|
|
Return values |
|
true |
The preference exists |
|
|
false |
The preference does NOT exist |
|
|
|
Example |
|
if (PlayerPrefs.HasKey("myPref")) {
// Preference exists
} else {
// Preference does not exist
}
|
<- Previous
Next ->
Save
|
void |
Save() |
|
|
Description |
|
This method will Save the current preference values.
If AutoSave(false) has been used then this method MUST be called in order to save the preferences.
|
|
|
Parameters |
|
none |
|
|
|
Return values |
|
none |
|
|
|
|
Example |
|
PlayerPrefs.Save(); // Save the current preference values
|
<- Previous
Next ->
Set
|
void |
Set(string key, T value) |
void |
SetDataType(string key, T value) |
|
|
Description |
|
Sets a preference value for the specified key. if the SetDataType method is used DataType should be replaced with a supported data type. See Example Below. |
|
|
Parameters |
|
string key |
The name of the preference to set |
|
T value |
The value for the preference where T is one of the supported data types |
|
|
Return values |
|
none |
|
|
|
|
Example |
|
int score = 100;
PlayerPrefs.Set("score", score);
PlayerPrefs.SetInt("score", score);
Sets the preference 'score' to the value held in the score variable.
Note: these 2 calls are equivalent.
The SetDataType methods are included to maintain compatibility with Unity PlayerPrefs but should, generally, not need to be used.
|
<- Previous
Next ->
<- Previous
Next ->
<- Previous
Next ->
Supported Datatypes
Player Prefs Lite supports many more C# and Unity data types than are available via Unity PlayerPrefs
C# Data types
The following C# data types are supported by Player Prefs Lite
Primitive Data Types
byte
sbyte
char
ushort
short
uint
int
ulong
long
decimal
float
double
string
C# Classes
DateTime
TimeSpan
Arrays
T[]; where T is a C# primitive type
To have these data types extended please refer to Change Requests
<- Previous
Next ->
Unity Data types
The following Unity data types are supported by Player Prefs Lite
Vector2
Vector2Int
Vector3
Vector3Int
Vector4
Quaternion
Color
Color32
Rect
Rectint
The utility also supports
Arrays
T[] where T is one of the above.
To have these data types extended please refer to Change Requests
<- Previous
Next ->
<- Previous
Next ->