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 物理系统:关节(一)
下一篇: Unity Terrain(地形)(三)
JPF29
博客等级 码龄9年 341粉丝 232原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值