BoolPrefs

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "<javascript>" to "<syntaxhighlight lang="javascript">")
m (Text replace - "</javascript>" to "</syntaxhighlight>")
Line 21: Line 21:
 
}
 
}
 
return defaultValue;
 
return defaultValue;
}</javascript>
+
}</syntaxhighlight>
  
 
[[Category: Utility]]
 
[[Category: Utility]]
 
[[Category: Scripts]]
 
[[Category: Scripts]]
 
[[Category: JavaScript]]
 
[[Category: JavaScript]]

Revision as of 20:52, 10 January 2012

Author: Eric Haines (Eric5h5)

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?true:false;
}
 
static function GetBool (name : String, defaultValue : boolean) : boolean {
	if (PlayerPrefs.HasKey(name)) {
		return GetBool(name);
	}
	return defaultValue;
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox