BoolPrefs

From Unify Community Wiki
Jump to: navigation, search

Author: Eric Haines (Eric5h5)

Contents

Description

So you're missing SetBool and GetBool from PlayerPrefs? Worry not, here they are!

Usage

Have this script somewhere in your project. Ideally it should be in Standard Assets/Scripts so it can easily be used by C# and Boo scripts. Call it PlayerPrefsX, and then you can use PlayerPrefsX.GetBool and PlayerPrefsX.SetBool as you would expect. Normally GetBool returns false if the key doesn't exist, but you can override that by optionally specifying a default value. i.e., "var foo = PlayerPrefsX.GetBool("blah", true);" will cause foo to be true if the key "blah" doesn't exist. These functions use PlayerPrefs.SetInt and GetInt behind the scenes (0 = false, 1 = true), so they're basically convenience functions.

PlayerPrefsX.js

static function SetBool (name : String, value : boolean) {
	PlayerPrefs.SetInt(name, value? 1 : 0);
}
 
static function GetBool (name : String) : boolean {
	return PlayerPrefs.GetInt(name) == 1;
}
 
static function GetBool (name : String, defaultValue : boolean) : boolean {
	if (PlayerPrefs.HasKey(name)) {
		return GetBool(name);
	}
	return defaultValue;
}

PlayerPrefsX.cs

public class PlayerPrefsX
{
	public static void SetBool(string name, bool booleanValue) 
	{
		PlayerPrefs.SetInt(name, booleanValue ? 1 : 0);
	}
 
	public static bool GetBool(string name)  
	{
	    return PlayerPrefs.GetInt(name) == 1 ? true : false;
	}
 
	public static bool GetBool(string name, bool defaultValue)
	{
	    if(PlayerPrefs.HasKey(name)) 
		{
	        return GetBool(name);
	    }
 
	    return defaultValue;
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox