Unity 四、常用脚本(十六)PlayerPrefs本地存储

PlayerPrefs

将数据存储到本地或对存储到本地的数据进行读取。 

public static void DeleteAll(); Removes all keys and values from the preferences. Use with caution. 删除所有键和值。谨慎使用。
public static void DeleteKey(string key); Removes key and its corresponding value from the preferences. 移除键及其对应值。
public static float GetFloat(string key, [DefaultValue("0.0F")] float defaultValue); Returns the value corresponding to key in the preference file if it exists.  返回key对应的值(如果存在)。 
public static float GetFloat(string key);
public static int GetInt(string key, [DefaultValue("0")] int defaultValue);
public static int GetInt(string key);
public static string GetString(string key, [DefaultValue("\"\"")] string defaultValue);
public static string GetString(string key);
public static bool HasKey(string key); Returns true if key exists in the preferences. 如果键存在,则返回true。
public static void Save(); Writes all modified preferences to disk. 将所有修改后的数据写入磁盘。
public static void SetFloat(string key, float value); Sets the value of the preference identified by key. 设置key标识的数据的值。
public static void SetInt(string key, int value);
public static void SetString(string key, string value);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestPlayerPrefs : MonoBehaviour
{
	void Start ()
    {
        PlayerPrefs.SetFloat("浮点数键",0.5f);
        PlayerPrefs.SetInt("整数键", 8);
        PlayerPrefs.SetString("字符串键", "aaa");

        PlayerPrefs.Save();

        bool b = PlayerPrefs.HasKey("字符串键");
        Debug.Log(b);

        float f = PlayerPrefs.GetFloat("浮点数键");
        Debug.Log(f);
        int i = PlayerPrefs.GetInt("整数键");
        Debug.Log(i);
        string s = PlayerPrefs.GetString("字符串键");
        Debug.Log(s);

        PlayerPrefs.DeleteKey("字符串键");
        string s2 = PlayerPrefs.GetString("字符串键");
        Debug.Log(s2);

        PlayerPrefs.DeleteAll();
        float f2 = PlayerPrefs.GetFloat("浮点数键");
        Debug.Log(f2);
        int i2 = PlayerPrefs.GetInt("整数键");
        Debug.Log(i2);
    }
}

Unity 四、常用脚本(十六)PlayerPrefs本地存储