如果首选项文件中存在与 key
对应的值,则返回该值。
如果不存在,PlayerPrefs.GetFloat 将返回 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); } }