BoolPrefs

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(PlayerPrefsX.js)
m (Fixed syntax highlighting tag, tidied formatting)
Line 23: Line 23:
 
}</syntaxhighlight>
 
}</syntaxhighlight>
  
[[Category: Utility]]
+
== PlayerPrefsX.cs ==
[[Category: Scripts]]
+
<syntaxhighlight lang="csharp">
[[Category: JavaScript]]
+
public class PlayerPrefsX
 
+
{
 
+
public static void SetBool(string name, bool booleanValue)  
 
+
 
+
same code in c#  (please excuse my lack of knowledge on how to properly edit wiki's)
+
 
+
<syntaxhighlight>
+
public static void SetBool (String name, bool booleanValue)  
+
 
{
 
{
    PlayerPrefs.SetInt(name, booleanValue?1:0);
+
PlayerPrefs.SetInt(name, booleanValue ? 1 : 0);
 
}
 
}
+
 
public static bool GetBool (String name)   
+
public static bool GetBool(string name)   
 
{
 
{
    return PlayerPrefs.GetInt(name)==1?true:false;
+
    return PlayerPrefs.GetInt(name) == 1 ? true : false;
 
}
 
}
+
 
public static bool GetBool (String name, bool defaultValue)
+
public static bool GetBool(string name, bool defaultValue)
 
{
 
{
    if (PlayerPrefs.HasKey(name))  
+
    if(PlayerPrefs.HasKey(name))  
 
{
 
{
 
        return GetBool(name);
 
        return GetBool(name);
 
    }
 
    }
 +
 
    return defaultValue;
 
    return defaultValue;
 
}
 
}
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
[[Category: Utility]]
 +
[[Category: Scripts]]
 +
[[Category: JavaScript]]

Revision as of 13:49, 17 January 2012

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?true:false;
}
 
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