PlayerPrefs Extensions
Set String
This method Sets a string value to PlayerPrefs using the specified key. The string can optionally be encrypted using a custom key for added security.
PrefExtensions.SetString("username", "Player1");
Get String
This method retrieves a string from PlayerPrefs using the specified key. If the key doesn't exist, it returns an empty string. It decrypts the string if it was Setd in an encrypted format.
string username = PrefExtensions.GetString("username");
Delete String
This method deletes a string value from PlayerPrefs associated with the specified key. Useful for removing stored data when it's no longer needed.
PrefExtensions.DeleteString("username");
Has String
This method checks if a string value exists in PlayerPrefs for the given key. It returns true if the key is found, otherwise false.
bool hasUsername = PrefExtensions.HasString("username");
Set Char
This method Sets a character value to PlayerPrefs under the specified key. The character is converted to a string and can be encrypted for additional protection.
PrefExtensions.SetChar("playerChar", 'A');
Get Char
This method Gets a character from PlayerPrefs using the specified key. If the key is missing, it returns a default character like \0 or 'B'.
char playerChar = PrefExtensions.GetChar("playerChar", 'B');
Delete Char
This method deletes a character value from PlayerPrefs for the specified key.
PrefExtensions.DeleteChar("playerChar");
Has Char
This method checks whether a character value exists in PlayerPrefs for the given key.
bool hasPlayerChar = PrefExtensions.HasChar("playerChar");
Set Int
This method Sets an integer value to PlayerPrefs. The integer is converted to a string and encrypted for security before saving.
PrefExtensions.SetInt("highScore", 100);
Get Int
This method Gets an integer value from PlayerPrefs for the given key. If the key does not exist, it returns a default value like 0.
int highScore = PrefExtensions.GetInt("highScore", 0);
Delete Int
This method deletes an integer value from PlayerPrefs associated with the specified key.
PrefExtensions.DeleteInt("highScore");
Has Int
This method checks whether an integer value exists in PlayerPrefs for the specified key.
bool hasHighScore = PrefExtensions.HasInt("highScore");
Set Float
This method Sets a float value to PlayerPrefs using the specified key. The float value is encrypted if a custom key is provided.
PrefExtensions.SetFloat("playerSpeed", 5.5f);
Get Float
This method retrieves a float from PlayerPrefs. If the key does not exist, it returns a default value like 0f or 3.0f.
float playerSpeed = PrefExtensions.GetFloat("playerSpeed", 3.0f);
Delete Float
This method deletes a float value from PlayerPrefs for the specified key.
PrefExtensions.DeleteFloat("playerSpeed");
Has Float
This method checks if a float value exists for a given key in PlayerPrefs.
bool hasSpeed = PrefExtensions.HasFloat("playerSpeed");
Set Bool
This method Sets a boolean value to PlayerPrefs using a key. The boolean value is converted to a string and can be encrypted.
PrefExtensions.SetBool("isGamePaused", true);
Get Bool
This method retrieves a boolean from PlayerPrefs. If the key is missing, it returns a default value like false.
bool isPaused = PrefExtensions.GetBool("isGamePaused", false);
Delete Bool
This method deletes a boolean value from PlayerPrefs for the specified key.
PrefExtensions.DeleteBool("isGamePaused");
Has Bool
This method checks if a boolean value exists for the given key in PlayerPrefs.
bool hasPauseSetting = PrefExtensions.HasBool("isGamePaused");
Set Double
This method Sets a double value to PlayerPrefs. It converts the double to a string and encrypts it if needed.
PrefExtensions.SetDouble("playerHealth", 100.5);
Get Double
This method retrieves a double from PlayerPrefs. If the key does not exist, it returns a default value like 0d or 50.0.
double playerHealth = PrefExtensions.GetDouble("playerHealth", 50.0);
Delete Double
This method deletes a double value from PlayerPrefs for the specified key.
PrefExtensions.DeleteDouble("playerHealth");
Has Double
This method checks if a double value exists in PlayerPrefs for the given key.
bool hasHealthSetting = PrefExtensions.HasDouble("playerHealth");
Set Long
This method Sets a long integer value to PlayerPrefs. The long value is encrypted before storing it if a custom key is provided.
PrefExtensions.SetLong("gameScore", 1000L);
Get Long
This method retrieves a long value from PlayerPrefs. If the key is not found, it returns a default value like 0L.
long gameScore = PrefExtensions.GetLong("gameScore", 0L);
Delete Long
This method deletes a long integer value from PlayerPrefs for the specified key.
PrefExtensions.DeleteLong("gameScore");
Has Long
This method checks if a long value exists for the specified key in PlayerPrefs.
bool hasScoreSetting = PrefExtensions.HasLong("gameScore");
Set Short
This method Sets a short value to PlayerPrefs with optional encryption using a custom key.
PrefExtensions.SetShort("playerLives", 5);
Get Short
Retrieves a short value from PlayerPrefs. Returns a default value like 0 if the key is not found.
short playerLives = PrefExtensions.GetShort("playerLives", 0);
Delete Short
Deletes the short value from PlayerPrefs for the given key.
PrefExtensions.DeleteShort("playerLives");
Has Short
Checks if a short value exists for the given key in PlayerPrefs.
bool hasLives = PrefExtensions.HasShort("playerLives");
Set Byte
Sets a byte value to PlayerPrefs, with optional EncryptExtensions.
PrefExtensions.SetByte("playerLevel", 2);
Get Byte
Gets a byte value from PlayerPrefs, returning a default value if the key does not exist.
byte playerLevel = PrefExtensions.GetByte("playerLevel", 0);
Delete Byte
Deletes the byte value from PlayerPrefs for the given key.
PrefExtensions.DeleteByte("playerLevel");
Has Byte
Checks if a byte value exists for the specified key in PlayerPrefs.
bool hasLevel = PrefExtensions.HasByte("playerLevel");
Set Vector2
Sets a Vector2 (X, Y) to PlayerPrefs by storing each component (x, y) separately.
PrefExtensions.SetVector2("playerPosition", new Vector2(10.0f, 20.0f));
Get Vector2
Gets a Vector2 from PlayerPrefs, returning the default value if the key does not exist.
Vector2 playerPosition = PrefExtensions.GetVector2("playerPosition", new Vector2(0.0f, 0.0f));
Delete Vector2
Deletes the Vector2 value (both x and y components) from PlayerPrefs for the given key.
PrefExtensions.DeleteVector2("playerPosition");
Has Vector2
Checks if a Vector2 exists by confirming both x and y components are stored in PlayerPrefs.
bool hasPosition = PrefExtensions.HasVector2("playerPosition");
Set Vector3
Sets a Vector3 (X, Y, Z) to PlayerPrefs by storing each component (x, y, z) separately.
PrefExtensions.SetVector3("playerPosition3D", new Vector3(10.0f, 20.0f, 30.0f));
Get Vector3
Gets a Vector3 from PlayerPrefs, returning a default value if the key is missing.
Vector3 playerPosition3D = PrefExtensions.GetVector3("playerPosition3D", new Vector3(0.0f, 0.0f, 0.0f));
Delete Vector3
Deletes the Vector3 value (x, y, z components) from PlayerPrefs for the specified key.
PrefExtensions.DeleteVector3("playerPosition3D");
Has Vector3
Checks if a Vector3 exists by confirming all x, y, and z components are stored.
bool hasPosition3D = PrefExtensions.HasVector3("playerPosition3D");
Set Vector4
Sets a Vector4 (X, Y, Z, W) to PlayerPrefs, storing each component separately.
PrefExtensions.SetVector4("rotation", new Vector4(1.0f, 0.0f, 0.0f, 1.0f));
Get Vector4
Gets a Vector4 from PlayerPrefs, returning a default value if not found.
Vector4 rotation = PrefExtensions.GetVector4("rotation", new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
Delete Vector4
Deletes the Vector4 value (x, y, z, w components) from PlayerPrefs for the specified key.
PrefExtensions.DeleteVector4("rotation");
Has Vector4
Checks if a Vector4 exists by confirming all four components (x, y, z, w) are present.
bool hasRotation = PrefExtensions.HasVector4("rotation");
Set Array
This method Sets an array of any type to PlayerPrefs by storing the length and each element separately. Optionally, you can encrypt the data using a custom key.
PrefExtensions.SetArray("inventoryItems", new string[] { "sword", "shield", "potion" });
Get Array
Gets an array of any type from PlayerPrefs. If the array doesn't exist, it returns the default value provided (e.g., new string[] { "none" }).
string[] inventoryItems = PrefExtensions.GetArray("inventoryItems", new string[] { "none" });
Delete Array
Deletes an array from PlayerPrefs by removing the length and each element that was saved.
PrefExtensions.DeleteArray<string>("inventoryItems");
Has Array
Checks if an array exists in PlayerPrefs by verifying if the length of the array is stored.
bool hasInventoryItems = PrefExtensions.HasArray<string>("inventoryItems");
Set Dictionary
Sets a dictionary of key-value pairs (of any type) to PlayerPrefs. It stores the count of dictionary entries, and each key and value separately.
PrefExtensions.SetDictionary("playerScores", new Dictionary<string, int> {
{ "player1", 100 },
{ "player2", 200 }
});
Get Dictionary
Gets a dictionary from PlayerPrefs. If the dictionary doesn't exist, it returns the default dictionary provided (e.g., { { "none", 0 } }).
Dictionary<string, int> playerScores = PrefExtensions.GetDictionary("playerScores", new Dictionary<string, int> { { "none", 0 } });
Delete Dictionary
Deletes a dictionary from PlayerPrefs by removing the length, keys, and values stored.
PrefExtensions.DeleteDictionary<string, int>("playerScores");
Has Dictionary
Checks if a dictionary exists in PlayerPrefs by verifying if the length of the dictionary is stored.
bool hasPlayerScores = PrefExtensions.HasDictionary<string, int>("playerScores");
Clear All
This method deletes all PlayerPrefs data, including arrays, dictionaries, and individual values.
WARNING: THIS ACTION IS IRREVERSIBLE!
PrefExtensions.ClearAll();
Last updated