WithPrefs

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "<javascript>" to "<syntaxhighlight lang="javascript">")
Line 8: Line 8:
 
myFloat = SliderWithPref("My Float", myFloat, 0.0, 1.0);
 
myFloat = SliderWithPref("My Float", myFloat, 0.0, 1.0);
  
<javascript>
+
<syntaxhighlight lang="javascript">
 
static function ToggleWithPref(name : String, defaultValue : boolean) : boolean {
 
static function ToggleWithPref(name : String, defaultValue : boolean) : boolean {
 
var oldValue : boolean = PlayerPrefsx.GetBool(name, defaultValue);
 
var oldValue : boolean = PlayerPrefsx.GetBool(name, defaultValue);

Revision as of 20:48, 10 January 2012

by StarManta

Instead of coding many switches and sliders and manually hook them up with preferences, these functions hook up easily with preferences. These use PlayerPrefsx but can easily be converted to use plain PlayerPrefs if desired.

Usage:

myVar = ToggleWithPref("My Variable", myVar); myFloat = SliderWithPref("My Float", myFloat, 0.0, 1.0);

static function ToggleWithPref(name : String, defaultValue : boolean) : boolean {
	var oldValue : boolean = PlayerPrefsx.GetBool(name, defaultValue);
	var newValue : boolean = GUILayout.Toggle(oldValue, name);
	if (newValue != oldValue) {
		PlayerPrefsx.SetBool(name, newValue);
	}
	return newValue;
}
 
 
static function SliderWithPref(name : String, defaultValue : float, min : float, max : float) : float {
	var oldValue : float = PlayerPrefsx.GetFloat(name, defaultValue);
	var newValue : float = GUILayout.Slider(oldValue, min, max);
	if (newValue != oldValue) {
		PlayerPrefsx.SetFloat(name, newValue);
	}
	return newValue;
}
</javascript>
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox