Idiots Guide To Player Prefs

I've got to the end of a game but I'm stuck on PlayerPrefs. I have read the info at http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.htmll and http://www.unifycommunity.com/wiki/index.php?title=ArrayPrefs2 but is there an idiots guide!

I've been trying to get my head round player prefs, and want to load the last level I was on when restarting a stand alone game. Not sure I've really grasped it, so I'll outline what I think is right, in the hope that someone will kindly say whether I'm right or wrong.

I've got the following javascript but don't know where I should save it or which game item to attach it to:

static var levelvar: int;   
//saving playerPrefs
function saveLevels()
{
PlayerPrefs.SetInt("SavedLevel",levelvar);
} 
//geting playerPrefs
function getScores()
{
PlayerPrefs.GetInt("savedLevel");
}

Presumably (I think. Please tell me if I'm wrong) in the script that moves me from level 1 to level 2 I include something like

PlayerPrefs.SetInt("savedLevel",2);
Application.LoadLevel (2); 

And then, if I close the game whilst still on level 2, on re-opening the game later, in the first code to run (what would that be?) I add something like

function getScores()
{
PlayerPrefs.GetInt("savedLevel");
}
if savedLevel== 2;
Application.LoadLevel (2);

I've got two parts to this single question about player prefs.

  1. Are the above scripts right. If not, what would be right?

  2. Where are these scripts actually put?

Could someone please advise me if the above works, or if I've completely lost the plot and got it all wrong.

When you use GetInt you have to assign it to something.

e.g.

levelToLoad = PlayerPrefs.GetInt("savedLevel");

Just calling GetInt wont do anything...

If you wanted to load a saved level at the start of a game you can put the GetInt directly inside the Application.LoadLevel() argument because that function returns an int.

e.g.

Application.LoadLevel(PlayerPrefs.GetInt("savedLevel"));

To test that you've actually got a saved level you can use the HasKey funtion like so:

if(PlayerPrefs.HasKey("savedLevel")){
   // there is a saved level, load that one
   Application.LoadLevel(PlayerPrefs.GetInt("savedLevel"));
}else{
   // no saved level, load the first one
   Application.LoadLevel(1);
}

I’ve just cooked this up and thought I’d leave it here incase anyone wants generic helper functions for PlayerPrefs. Enjoy :slight_smile:

		/// <summary>
		/// Sets <variable> to <value> based on its type T and additionally
		/// stores it in the registry or plist using key <varName>
		/// </summary>
		/// <auth: Isaac Dart (isaac@mantle.tech) >
		public static void SetPersistentVar<T>(string varName, ref T variable, T value)  {

			variable = value;

			Type varType = variable.GetType() ;
			if (varType == typeof(int)) {
				int intVal = Convert.ToInt32(variable);
				PlayerPrefs.SetInt("i_" + varName, intVal);
			} 
			else if (varType == typeof(bool)) {
				int intVal = Convert.ToInt32(variable);
				PlayerPrefs.SetInt("b_" + varName, intVal);
			}
			else if (varType == typeof(float)) {
				float floatVal = (float)(Convert.ToDouble(variable));
				PlayerPrefs.SetFloat("f_" + varName, floatVal);
			}
			else {
				string stringVal = Convert.ToString(variable);
				PlayerPrefs.SetString("s_" + varName, stringVal);
			}

			PlayerPrefs.Save();

		}

		/// <summary>
		/// Returns a value of type T from the registry or plist using <varName>
		/// </summary>
		/// <auth: Isaac Dart (isaac@mantle.tech) >
		public static T GetPersistentVar<T>(string varName, T defaultValue)  {

			T variable = defaultValue;

			Type varType = variable.GetType() ;
			if (varType == typeof(int)) {
				int defaultIntVal = Convert.ToInt32(defaultValue);
				int intVal = PlayerPrefs.GetInt("i_" + varName, defaultIntVal);
				variable = (T)Convert.ChangeType(intVal, varType);
			} 
			else if (varType == typeof(bool)) {
				int defaultIntVal = Convert.ToInt32(defaultValue);
				int intVal = PlayerPrefs.GetInt("b_" + varName, defaultIntVal);
				bool boolVal = intVal != 0;
				variable = (T)Convert.ChangeType(boolVal, varType);
			}
			else if (varType == typeof(float)) {
				float defaultFloatVal = (float)(Convert.ToDouble(defaultValue));
				float floatVal = PlayerPrefs.GetFloat("f_" + varName, defaultFloatVal);
				variable = (T)Convert.ChangeType(floatVal, varType);
			}
			else {
				string defaultStringVal = Convert.ToString(defaultValue);
				string stringVal = PlayerPrefs.GetString("s_" + varName, defaultStringVal);
				variable = (T)Convert.ChangeType(stringVal, varType);
			}

			return variable;

		}

Also check out PlayerPrefsx code for storing more complex datastructures.