WithPrefs

From Unify Community Wiki
Jump to: navigation, search

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;
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox