Unity游戏存档-PlayerPrefs类

对于游戏存档除了XML,Json,Sqlite,unity提供了一个类可以用来存储数据,那就是PlayerPrefs。

这个类对应的API如下

Class Functions类函数

  • Sets the value of the preference identified by key.
    设置由key确定的参数值。
  • Returns the value corresponding to key in the preference file if it exists.
    如果存在,返回偏好文件中key对应的值。
  • Sets the value of the preference identified by key.
    设置由key确定的参数值。
  • Returns the value corresponding to key in the preference file if it exists.
    如果存在,返回游戏存档文件中key对应的值。
  • Sets the value of the preference identified by key.
    设置由key确定的参数值。
  • Returns the value corresponding to key in the preference file if it exists.
    如果存在,返回游戏存档文件中key对应的值。
  • Returns true if key exists in the preferences.
    如果key在游戏存档中存在,返回true。
  • Removes key and its corresponding value from the preferences.
    从游戏存档中删除key和它对应的值。 
  • Removes all keys and values from the preferences. Use with caution.
    从偏好中删除所有key。请谨慎使用。
  • Writes all modified preferences to disk.
    写入所有修改参数到硬盘。
PlayerPrefs类的用法概要:

1.存储的数据类型只支持int,float,string三种类型。

2.在unity中该类是使用键值对存储数据的。

3.当通过键名来读取值得时候,如果对应的值不存在,就返回默认值。

4.基本用法:

PlayerPrefs.SetString("Name",mName);  
PlayerPrefs.SetInt("Age",mAge);  
PlayerPrefs.SetFloat("Grade",mGrade)  

PlayerPrefs.SetString("Name",mName);  
PlayerPrefs.SetInt("Age",mAge);  
PlayerPrefs.SetFloat("Grade",mGrade)  


下面用一个实例来说明具体的用法:

public class Playerprefs : MonoBehaviour
{
    public string set_NAME;
    public string get_NAME;

    void OnGUI()
    {
        GUILayout.BeginHorizontal("box");
        GUILayout.Label("姓名:");
        set_NAME = GUILayout.TextArea(set_NAME, 200, GUILayout.Width(50));
        if (GUILayout.Button("存储数据"))
        {

            //将我们输入的姓名保存到本地,命名为_NAME ;
            PlayerPrefs.SetString("_NAME", set_NAME);
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal("box");
        if (GUILayout.Button("读取数据"))
        {

            //读取本地数据中名称为_NAME 的数据;
            get_NAME = PlayerPrefs.GetString("_NAME");
        }
        GUILayout.Label("你输入的姓名:" + get_NAME);
        GUILayout.EndHorizontal();
    }
}


### UnityPlayerPrefs 的用法 `PlayerPrefs` 是 Unity 提供的一个轻量级存储工具,用于保存游戏中的小型数据(如设置选项、进度等)。它基于键值对的方式工作,在不同平台上会自动选择合适的存储方式。以下是 `PlayerPrefs` 的基本功能及其常见问题解决方案。 #### 基本操作方法 可以使用以下函数来读取和写入数据: - **写入整数型数据** 使用 `PlayerPrefs.SetInt(key, value)` 将整数值存入指定的键名下[^1]。 - **写入浮点型数据** 使用 `PlayerPrefs.SetFloat(key, value)` 存储浮点数值。 - **写入字符串数据** 使用 `PlayerPrefs.SetString(key, value)` 来保存字符串型的值。 每次修改后需调用 `PlayerPrefs.Save()` 手动触发保存动作以确保数据被持久化到磁盘上[^2]。 ```csharp // 设置玩家的名字并保存 PlayerPrefs.SetString("playerName", "JohnDoe"); PlayerPrefs.Save(); // 获取之前设定好的名字,默认返回为空字符串如果不存在该键的话 string playerName = PlayerPrefs.GetString("playerName", ""); ``` #### 数据检索过程 通过对应的获取函数可以从本地恢复先前储存的数据项: - 整形变量可通过 `int retrievedValue = PlayerPrefs.GetInt("keyName");` 获得; - 浮点数则利用 `float retrievedFloat = PlayerPrefs.GetFloat("keyName");`; - 字符串形式的内容借助于命令 `string retrievedString = PlayerPrefs.GetString("keyName");`. 注意当尝试访问未定义过的 key 时将会得到默认值零或者空字串取决于所请求的是哪种型的数据[^3]. #### 解决方案针对可能遇到的问题 ##### 问题一:跨平台兼容性差异 由于各操作系统对于偏好设置文件处理机制存在区别,因此可能导致某些特定环境下无法正常加载已存在的配置记录。例如 iOS 设备上的沙盒环境限制使得应用卸载后再重新安装之后原有的 playerprefs 文件会被清除掉[^4]。 解决办法是在设计应用程序逻辑之初就考虑到这一点,并提供额外手段比如云端同步服务作为补充措施以防重要资料丢失。 ##### 问题二:安全性不足隐患 因为 playerprefs 实际是以明文形式存在于设备内部目录下的 xml 文档之中,所以很容易遭到恶意篡改攻击者可以直接编辑这些公开暴露出来的参数从而破坏整个系统的稳定性甚至造成作弊现象发生[^5]。 对此建议开发者们仅限把那些无关紧要的信息放入其中而对于敏感性的内容最好采用加密算法加以保护然后再传送到远程服务器端进行长期保管而不是单纯依赖 local storage alone. --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值