- Home /
Player prefs and bool
I'm having a hard time figuring out how to use player prefs to save and load the game(automatic at game start) in c#.
It doesn't help that most of the things that should be saved are of the bool type, and it only seems to take int and strings, do I really have to convert all my bools if so, what would be the cleanest way to do that?
So how would one go saving and loading bool types?
Answer by Eric5h5 · Apr 07, 2011 at 08:42 PM
You can use BoolPrefs (still uses GetInt/SetInt, but you don't have to think about it).
Link is long gone but this is what it probably looks like if anyone's wondering:
using UnityEngine;
using System.Collections;
public class PlayerPrefs2
{
public static void SetBool(string key, bool state)
{
PlayerPrefs.SetInt(key, state ? 1 : 0);
}
public static bool GetBool(string key)
{
int value = PlayerPrefs.GetInt(key);
if (value == 1)
{
return true;
}
else
{
return false;
}
}
}
The page is at http://wiki.unity3d.com/index.php/BoolPrefs now. The actual script is much shorter...the GetBool function can easily be done in 1 line. ;)
Answer by CryptoGrounds · Jun 13, 2019 at 11:21 PM
Just convert to string:
bool tempbool = true;
PlayerPrefs.SetString("testBool", tempbool.ToString());
tempbool = Boolean.Parse(PlayerPrefs.GetString("testBool", "true"));
This didn't work for me. The get string part where you say true just makes it true. I have a bool that can change depending on user input.
Answer by letroll · Oct 02, 2015 at 09:13 AM
Or you can create this extension:
using UnityEngine;
using System.Collections;
public static class PlayerPrefExtension
{
public static bool GetBool (this PlayerPrefs playerPrefs, string key)
{
return PlayerPrefs.GetInt (key) == 1;
}
public static void SetBool (this PlayerPrefs playerPrefs, string key, bool state)
{
PlayerPrefs.SetInt (key, state ? 1 : 0);
}
}
If I recall correctly, extension methods require an instance of an object: will not work on static classes, so that is not possible.
Answer by Ricna · Jul 30, 2014 at 03:18 AM
Bool is nothing more than yes or no, true or false, something or zero.. So, considering a simple C program.. I usually use 2 methods to access 1 attribute.
Example:
public class User{
private string FIELD_PREMIUM = "premiumAccount";
private bool premiumAccount;
public User(){
this.LoadUser();
}
public void LoadUser(){
this.premiumAccount = PlayerPrefs.GetInt(FIELD_PREMIUM,0)>0?true:false;
}
public void PremiumAccountPurchased(){
this.premiumAccount = true;
this.Save();
}
public bool HasPremiumAccount(){
this.LoadUser();
return this.premiumAccount;
}
public void Save(){
PlayerPrefs.SetInt(FIELD_PREMIUM, this.premiumAccount?1:0);
}
}
All that you need is:
To get the value: bool value = PlayerPrefs.GetInt("somefield",0)>0?true:false;
To set the value: PlayerPrefs.SetInt("somefield", myBoolValue?1:0);
I hope this helps your or someone :)
Thank you for your reply. this is the one that worked for me out of the couple ways I tried from other people that replied to this post.
Answer by MianAbdul · Mar 21, 2018 at 02:54 PM
"Get" by using this line: bool myBool = PlayerPrefs.GetInt ("VariableName") == 1 ? true : false;
"Set" by using this line: PlayerPrefs.SetInt ("VariableName", true ? 1 : 0);