PlayerPrefs
Static Methods
DeleteAll | Removes all keys and values from the preferences. Use with caution. |
---|---|
DeleteKey | Removes key and its corresponding value from the preferences. |
GetFloat | Returns the value corresponding to key in the preference file if it exists. |
GetInt | Returns the value corresponding to key in the preference file if it exists. |
GetString | Returns the value corresponding to key in the preference file if it exists. |
HasKey | Returns true if key exists in the preferences. |
Save | Writes all modified preferences to disk. |
SetFloat | Sets the value of the preference identified by key. |
SetInt | Sets the value of the preference identified by key. |
SetString | Sets the value of the preference identified by key. |
PlayerPrefs.SetString
public static void SetString(string key, string value);
//Attach this script to a GameObject in the Scene to set up the PlayerPrefs.
using UnityEngine;
using UnityEngine.SceneManagement;
public class SetUpPlayerPrefsExample : MonoBehaviour
{
string m_PlayerName;
void Start()
{
m_PlayerName = "Enter Your Name";
}
void Update()
{
//Give the PlayerPrefs some values to send over to the next Scene
PlayerPrefs.SetString("Name", m_PlayerName);
}
void OnGUI()
{
//Create a Text Field where the user inputs their name
m_PlayerName = GUI.TextField(new Rect(10, 10, 200, 20), m_PlayerName, 25);
//Create a button which loads the appropriate level when you press it
if (GUI.Button(new Rect(10, 30, 200, 60), "Next Scene"))
{
SceneManager.LoadScene("Scene2");
}
}
}
PlayerPrefs.SetInt
public static void SetInt(string key, int value);
//Attach this script to a GameObject in the Scene to set up the PlayerPrefs.
using UnityEngine;
using UnityEngine.SceneManagement;
public class SetUpPlayerPrefsExample : MonoBehaviour
{
void Start()
{
//Give the PlayerPrefs some values to send over to the next Scene
PlayerPrefs.SetInt("Score", 20);
}
void OnGUI()
{
//Create a button which loads the appropriate level when you press it
if (GUI.Button(new Rect(10, 30, 200, 60), "Next Scene"))
{
SceneManager.LoadScene("Scene2");
}
}
}
PlayerPrefs.SetFloat
public static void SetFloat(string key, float value);
/This script sets the float up in the PlayerPrefs to be used elsewhere. Use GetFloat to return the settings.
using UnityEngine;
using UnityEngine.SceneManagement;
public class Example : MonoBehaviour
{
void Start()
{
//Give the PlayerPrefs some values to send over
PlayerPrefs.SetFloat("Health", 50.0F);
}
void OnGUI()
{
//Create a button which loads the appropriate level when you press it
if (GUI.Button(new Rect(100, 200, 200, 60), "Next Scene"))
{
SceneManager.LoadScene("Scene2");
}
}
}
PlayerPrefs.GetString
public static string GetString(string key);
public static string GetString(string key, string defaultValue);
//Use this script to fetch the settings and show them as text on the screen.
using UnityEngine;
using UnityEngine.UI;
public class PlayerPrefsDeleteAllExample : MonoBehaviour
{
string m_PlayerName;
void Start()
{
//Fetch the PlayerPref settings
SetText();
}
void SetText()
{
//Fetch name (string) from the PlayerPrefs (set these Playerprefs in another script). If no string exists, the default is "No Name"
m_PlayerName = PlayerPrefs.GetString("Name", "No Name");
}
void OnGUI()
{
//Fetch the PlayerPrefs settings and output them to the screen using Labels
GUI.Label(new Rect(50, 50, 200, 30), "Name : " + m_PlayerName);
}
}
PlayerPrefs.GetInt
public static int GetInt(string key);
public static int GetInt(string key, int defaultValue);
//Use this script to fetch the settings and show them as text on the screen.
using UnityEngine;
using UnityEngine.UI;
public class PlayerPrefsDeleteAllExample : MonoBehaviour
{
int m_Score;
void Start()
{
//Fetch the PlayerPref settings
SetText();
}
void SetText()
{
//Fetch the score from the PlayerPrefs (set these Playerprefs in another script). If no Int of this name exists, the default is 0.
m_Score = PlayerPrefs.GetInt("Score", 0);
}
void OnGUI()
{
//Fetch the PlayerPrefs settings and output them to the screen using Labels
GUI.Label(new Rect(50, 130, 200, 30), "Score : " + m_Score);
}
}
PlayerPrefs.GetFloat
public static float GetFloat(string key);
public static float GetFloat(string key, float defaultValue);
//Use this script to fetch the PlayerPrefs settings and show them as text on the screen.
using UnityEngine;
using UnityEngine.UI;
public class PlayerPrefsDeleteAllExample : MonoBehaviour
{
float m_Health;
void Start()
{
//Fetch the PlayerPref settings
SetText();
}
void SetText()
{
//Fetch the health from the PlayerPrefs (set these Playerprefs elsewhere). If no float of this name exists, the default is 0
m_Health = PlayerPrefs.GetFloat("Health", 0);
}
void OnGUI()
{
//Fetch the PlayerPrefs settings and output them to the screen using Labels
GUI.Label(new Rect(50, 90, 200, 30), "Health : " + m_Health);
}
}