Created
June 20, 2014 09:13
-
-
Save TarasOsiris/d12063efdd486a03b706 to your computer and use it in GitHub Desktop.
Unity3D PlayerPrefs wrapper to provide saving boolean values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
/// <summary> | |
/// PlayerPrefs wrapper to provide saving boolean values. | |
/// </summary> | |
public static class CustomPlayerPrefs | |
{ | |
private const int StorageFalse = 0; | |
private const int StorageTrue = 1; | |
#region bool | |
public static void SetBool(string key, bool value, bool saveImmediately) | |
{ | |
int intValue = value ? StorageTrue : StorageFalse; | |
PlayerPrefs.SetInt(key, intValue); | |
if (saveImmediately) { Save(); } | |
} | |
public static bool GetBool(string key) | |
{ | |
int intValue = PlayerPrefs.GetInt(key); | |
return intValue == StorageTrue; | |
} | |
#endregion | |
#region int | |
public static void SetInt(string key, int value, bool saveImmediately) | |
{ | |
PlayerPrefs.SetInt(key, value); | |
if (saveImmediately) { Save(); } | |
} | |
public static int GetInt(string key) | |
{ | |
return PlayerPrefs.GetInt(key); | |
} | |
public static int GetInt(string key, int defaultValue) | |
{ | |
return PlayerPrefs.GetInt(key, defaultValue); | |
} | |
#endregion | |
#region float | |
public static void SetFloat(string key, float value, bool saveImmediately) | |
{ | |
PlayerPrefs.SetFloat(key, value); | |
if (saveImmediately) { Save(); } | |
} | |
public static float GetFloat(string key) | |
{ | |
return PlayerPrefs.GetFloat(key); | |
} | |
public static float GetFloat(string key, float defaultValue) | |
{ | |
return PlayerPrefs.GetFloat(key, defaultValue); | |
} | |
#endregion | |
#region string | |
public static void SetString(string key, string value, bool saveImmediately) | |
{ | |
PlayerPrefs.SetString(key, value); | |
if (saveImmediately) { Save(); } | |
} | |
public static string GetString(string key) | |
{ | |
return PlayerPrefs.GetString(key); | |
} | |
public static string GetString(string key, string defaultValue) | |
{ | |
return PlayerPrefs.GetString(key, defaultValue); | |
} | |
#endregion | |
public static bool HasKey(string key) | |
{ | |
return PlayerPrefs.HasKey(key); | |
} | |
public static void DeleteAll() | |
{ | |
PlayerPrefs.DeleteAll(); | |
} | |
public static void DeleteKey(string key) | |
{ | |
PlayerPrefs.DeleteKey(key); | |
} | |
#region private | |
private static void Save() | |
{ | |
PlayerPrefs.Save(); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment