OnGUI allows for toggles using a boolean.
However, PlayerPrefs has no SetBool.
I have been using SetInt to get around this… but this requires several lines of code.
Is there an elegant solution to storing a boolean through PlayPrefs?
Thanks.
OnGUI allows for toggles using a boolean.
However, PlayerPrefs has no SetBool.
I have been using SetInt to get around this… but this requires several lines of code.
Is there an elegant solution to storing a boolean through PlayPrefs?
Thanks.
Only one line of code, but still this is a bit more convenient: http://www.unifycommunity.com/wiki/index.php?title=BoolPrefs
–Eric
Thanks Eric.
This is exactly what I was looking for when I used the word “elegant”.
As an added bonus I had forgotten about the ability to use “value?1:0” format (also being elegant).
For quick reading below:
static function SetBool (name : String, value : boolean) {
PlayerPrefs.SetInt(name, value?1:0);
}
static function GetBool (name : String) : boolean {
return PlayerPrefs.GetInt(name)==1?true:false;
}
static function GetBool (name : String, defaultValue : boolean) : boolean {
if (PlayerPrefs.HasKey(name)) {
return GetBool(name);
}
return defaultValue;
}
Thanks.
Hi
I’m trying to use the above script. But when I use SetBool, and I check in my playerprefs.plist file, the ‘Type’ is listed as a String not a Boolean.
This is what I’m using to set the pref:
PlayerPrefsX.SetBool(“Hints”, true);
Something I’m doing wrong?
there is no inbuilt support to write bool, a fake can not change that as a fake can not change the engine code
I don’t even think it writes anything but strings actually (as above code actually writes int, not string yet if you see string it potentially saves string independent of what comes in)
Nothing is wrong; the type in the .plist file is irrelevant.
–Eric
Can I ask why SetBool isn’t supported? Just curious.
This is a fantastic workaround. Thank you for sharing.
Any idea if booleans are supported in PlayerPrefs in Unity 4.x?
Not that i am aware, i simply wrote a playerprefs extension