数据持久化
数据持久化是指将应用程序中的临时数据(如游戏进度、玩家设置等)永久保存到存储设备(硬盘、SD卡等)中的过程,以便在应用程序关闭后再次启动时能够恢复这些数据。

Playerprefs
PlayerPrefs是Unity提供的一个简单的键值对存储系统(键:string类型,值: int float string),用于在游戏会话之间保存和加载玩家偏好设置或简单游戏数据。它是Unity中最基础的数据持久化解决方案,是Unity提供的可以用于存储玩家数据的公共类
- 轻量级存储:适合存储少量简单数据(如设置、分数等)
- 跨平台:在所有Unity支持的平台上工作
- 自动持久化:数据会保存在设备本地
- 三种数据类型:支持int、float、string三种基本类型
关于Playerprefs最基础的用法是直接使用Set方法进行数据的存储,但是在运行的过程中只会存储在内存中,需要在结束时,Unity会自动将其存入硬盘中。如果游戏运行的过程中崩溃,那么则不会存储在硬盘中。可以使用自带的Api,Save方法进行自动存储。
1 2 3 4 5 6 7 8 9 10
| using UnityEngine; public class PlayerPrefsManager : MonoBehaviour{ void Start(){ PlayerPrefs.SetInt("Score", 100); PlayerPrefs.GetInt("Score"); PlayerPrefs.SetString("Name", "John"); PlayerPrefs.GetString("Name"); PlayerPrefs.SetFloat("Score", 100.5f); } }
|
同时,如果使用不同的数据类型对同一键名进行存储时,数据会进行覆盖。只要在运行时使用了Set方法的对应值,即使没有马上存储Save在本地,也能够读取出数据。如果找不到键对应的值,那么返回函数的默认值,如果找到,则不会返回默认值。
1
| float score = PlayerPrefs.GetFloat("Score",100)
|
删除数据:删除指定的键值和删除所有的存储的信息
1 2
| PlayerPrefs.DeleteKey("Score"); PlayerPrefs.DeleteAll();
|
例题

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerPrefsManager : MonoBehaviour { }
[System.Serializable] public class EquipmentInfo { public int id; public int count; }
[System.Serializable] public class PlayerInfo { public string name; public int age; public int attack; public int defense;
public List<EquipmentInfo> equipments = new List<EquipmentInfo>();
public void Save() { PlayerPrefs.SetString("PlayerName", name); PlayerPrefs.SetInt("PlayerAge", age); PlayerPrefs.SetInt("PlayerAttack", attack); PlayerPrefs.SetInt("PlayerDefense", defense);
PlayerPrefs.SetInt("EquipCount", equipments.Count); for (int i = 0; i < equipments.Count; i++) { PlayerPrefs.SetInt($"Equip_{i}_id", equipments[i].id); PlayerPrefs.SetInt($"Equip_{i}_count", equipments[i].count); } PlayerPrefs.Save(); }
public void Load() { name = PlayerPrefs.GetString("PlayerName", ""); age = PlayerPrefs.GetInt("PlayerAge", 0); attack = PlayerPrefs.GetInt("PlayerAttack", 0); defense = PlayerPrefs.GetInt("PlayerDefense", 0);
equipments.Clear(); int equipCount = PlayerPrefs.GetInt("EquipCount", 0); for (int i = 0; i < equipCount; i++) { EquipmentInfo equip = new EquipmentInfo(); equip.id = PlayerPrefs.GetInt($"Equip_{i}_id", 0); equip.count = PlayerPrefs.GetInt($"Equip_{i}_count", 0); equipments.Add(equip); } } }
|
存储位置
PlayerPrefs的数据存储位置因平台而异:
- Windows:注册表中(
HKEY_CURRENT_USER\Software\[公司名]\[产品名])
- Mac:
~/Library/Preferences/[bundle identifier].plist
- iOS/Android:应用的沙盒目录中
PlayerPrefs的唯一性
1.基于键(Key)的唯一性
- 键是唯一的标识符:每个键对应唯一的值
- 同键覆盖原则:对同一键多次赋值会覆盖前值
1 2
| PlayerPrefs.SetInt("Score", 100); PlayerPrefs.SetInt("Score", 200);
|
- 应用级别的唯一性
- 按应用隔离:数据只对当前应用唯一
- 不同应用不共享:即使在同一设备上,不同Unity应用的PlayerPrefs互不干扰
- 平台级别的唯一性
- 跨平台不共享:同一游戏在Windows、Android等不同平台的PlayerPrefs数据相互独立
- 同一平台不同安装:卸载重装后,PlayerPrefs数据通常会被清除(iOS/Android)