Contact Us SabreCSG Documentation

Player Prefs Editor & Utilities Documentation

PlayerPrefs Editor Guide

To open the PlayersPrefs Editor go to Window -> Player Prefs Editor

This will open an editor window which you can dock like any other Unity window.

THE PLAYER PREFS LIST

If you have existing saved player prefs you should see them listed in the main window. You can change the values just by changing the value text box, you can also delete one of these existing player prefs by clicking the X button on the right.

Changes in value and any action taken in the editor will update in real-time. Additions and deletions outside of the editor (creation/deletion of a new player pref by your code) will also be instant on Windows, but will take longer on OSX. This is because the mechanism which Unity uses to save its player prefs works differently on OSX and the editor relies on Unity to write to its plist. You can prompt Unity to save by clicking the Force Save button at the bottom right, but it may still take Unity a couple of seconds to write the file.

SEARCH

The editor supports filtering keys by entering a keyword in the search textbox at the top. As you type the search results will refine. Search is case-insensitive and if auto-decrypt is turned on it will also work with encrypted player prefs.

ADDING A NEW PLAYER PREF

At the bottom of the editor you’ll see a section for adding a new player pref. There are toggle options to determine what type it is and a checkbox for whether the player pref should be encrypted. Once you’ve selected the right settings and filled in a key and value hit the Add button to instantly add the player pref.

DELETING AN EXISTING PLAYER PREF

To delete an existing player pref, click the X button next to the player pref in the list. You can also delete all player prefs by clicking the Delete All button at the bottom of the editor.

AUTO DECRYPTION

The PlayerPrefs editor also supports working with the encryption methods that come in the corresponding PlayerPrefsUtility static class. Encrypted player prefs are colour tinted blue. By default their encrypted keys and values are shown, but if you check the auto-decryption checkbox the decrypted key and value will be shown. Note, if the key in SimpleEncryption changes after existing encrypted player prefs have been saved, decryption won’t be possible and these broken player prefs will be coloured red.

PlayerPrefs Utility

PlayerPrefsUtility is a static class that adds additional functionality that works on top of Unity’s in built PlayerPrefs system. It consists of two main sets of functionality: get and set methods for the built in PlayerPref types that use an encryption layer and secondly support for additional data types (beyond the built in float, int, string).

ENCRYPTION

The PlayerPrefsUtility works alongside a class called SimpleEncryption. SimpleEncryption is responsible for encrypting and decrypting strings and contains a private key that it uses with the encryption algorithm to encrypt and decrypt strings.

IMPORTANT: Make sure you change the SimpleEncryption key from it’s default value if you want your encryption key to be unique. Changing this key after existing encrypted player prefs are saved will require those existing player prefs to be deleted as it would no longer be possible to decrypt them.

Encryption example usage:

// Record score if it's the new best score (Encrypted)
if(thisScore > PlayerPrefsUtility.GetEncryptedFloat("BestScore"))
{
	PlayerPrefsUtility.SetEncryptedFloat("BestScore", thisScore);
}

// Add on the new coin balance (Encrypted)
int coinBalance = PlayerPrefsUtility.GetEncryptedInt("PlayerCoins");
coinBalance += coinsEarned;
PlayerPrefsUtility.SetEncryptedInt("PlayerCoins", coinBalance);

ADDITIONAL DATA TYPES

PlayerPrefsUtility also introduces support for four more data types, Enum, DateTime, TimeSpan and booleans.

// Mark the game as finished
PlayerPrefsUtility.SetBool("FinishedLastGame", true);

// Record the last map
PlayerPrefsUtility.SetEnum("MapLastFinished", currentMap);

// Record how long it took the player and when they finished
PlayerPrefsUtility.SetTimeSpan("TimeTaken", DateTime.UtcNow - startTime);
PlayerPrefsUtility.SetDateTime("FinishTime", DateTime.UtcNow);

PLAYERPREFSUTILITY API LISTING

This is a list of the public methods in PlayerPrefsUtility with their descriptive method summary:

public static class PlayerPrefsUtility
{
	/// <summary>
	/// Determines if the specified player pref key refers to an encrypted record
	/// </summary>
	public static bool IsEncryptedKey (string key)

	/// <summary>
	/// Decrypts the specified key
	/// </summary>
	public static string DecryptKey(string encryptedKey)

	/// <summary>
	/// Encrypted version of PlayerPrefs.SetFloat(), stored key and value is encrypted in player prefs
	/// </summary>
	public static void SetEncryptedFloat(string key, float value)

	/// <summary>
	/// Encrypted version of PlayerPrefs.SetInt(), stored key and value is encrypted in player prefs
	/// </summary>
	public static void SetEncryptedInt(string key, int value)

	/// <summary>
	/// Encrypted version of PlayerPrefs.SetString(), stored key and value is encrypted in player prefs
	/// </summary>
	public static void SetEncryptedString(string key, string value)

	/// <summary>
	/// Helper method that can handle any of the encrypted player pref types, returning a float, int or string based
	/// on what type of value has been stored.
	/// </summary>
	public static object GetEncryptedValue(string encryptedKey, string encryptedValue)

	/// <summary>
	/// Encrypted version of PlayerPrefs.GetFloat(), an unencrypted key is passed and the value is returned decrypted
	/// </summary>
	public static float GetEncryptedFloat(string key, float defaultValue = 0.0f)

	/// <summary>
	/// Encrypted version of PlayerPrefs.GetInt(), an unencrypted key is passed and the value is returned decrypted
	/// </summary>
	public static int GetEncryptedInt(string key, int defaultValue = 0)

	/// <summary>
	/// Encrypted version of PlayerPrefs.GetString(), an unencrypted key is passed and the value is returned decrypted
	/// </summary>
	public static string GetEncryptedString(string key, string defaultValue = "")

	/// <summary>
	/// Helper method to store a bool in PlayerPrefs (stored as an int)
	/// </summary>
	public static void SetBool(string key, bool value)

	/// <summary>
	/// Helper method to retrieve a bool from PlayerPrefs (stored as an int)
	/// </summary>
	public static bool GetBool(string key, bool defaultValue = false)

	/// <summary>
	/// Helper method to store an enum value in PlayerPrefs (stored using the string name of the enum)
	/// </summary>
	public static void SetEnum(string key, Enum value)

	/// <summary>
	/// Generic helper method to retrieve an enum value from PlayerPrefs and parse it from its stored string into the 
	/// specified generic type. This method should generally be preferred over the non-generic equivalent
	/// </summary>
	public static T GetEnum<T>(string key, T defaultValue = default(T)) where T: struct 

	/// <summary>
	/// Non-generic helper method to retrieve an enum value from PlayerPrefs (stored as a string). Default value must be
	/// passed, passing null will mean you need to do a null check where you call this method. Generally try to use the
	/// generic version of this method instead: GetEnum<T>
	/// </summary>
	public static object GetEnum(string key, Type enumType, object defaultValue)

	/// <summary>
	/// Helper method to store a DateTime (complete with its timezone) in PlayerPrefs as a string
	/// </summary>
	public static void SetDateTime(string key, DateTime value)

	/// <summary>
	/// Helper method to retrieve a DateTime from PlayerPrefs (stored as a string) and return a DateTime complete with
	/// timezone (works with UTC and local DateTimes)
	/// </summary>
	public static DateTime GetDateTime(string key, DateTime defaultValue = new DateTime())

	/// <summary>
	/// Helper method to store a TimeSpan in PlayerPrefs as a string
	/// </summary>
	public static void SetTimeSpan(string key, TimeSpan value)

	/// <summary>
	/// Helper method to retrieve a TimeSpan from PlayerPrefs (stored as a string)
	/// </summary>
	public static TimeSpan GetTimeSpan(string key, TimeSpan defaultValue = new TimeSpan())
}