unity3D之PlayerPrefs API详解 (英文版)来自官方总结

PlayerPrefs

Static Methods

DeleteAllRemoves all keys and values from the preferences. Use with caution.
DeleteKeyRemoves key and its corresponding value from the preferences.
GetFloatReturns the value corresponding to key in the preference file if it exists.
GetIntReturns the value corresponding to key in the preference file if it exists.
GetStringReturns the value corresponding to key in the preference file if it exists.
HasKeyReturns true if key exists in the preferences.
SaveWrites all modified preferences to disk.
SetFloatSets the value of the preference identified by key.
SetIntSets the value of the preference identified by key.
SetStringSets 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);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值