数据持久化 1 - PlayerPrefs

数据持久化

文章目录

PlayerPrefs 基本方法

  • PlayerPrefs Unity用于存储读取玩家数据的公共类

存储

PlayerPrefs的数据存储类似于键值对存储

提供了3种方法 int float string

c 复制代码
PlayerPrefs.SetInt("myInt", 1);
PlayerPrefs.SetString("myString", "str");
PlayerPrefs.SetFloat("myFloat", 10.1f);

// set方法只会把数据存储到内存中
// 游戏结束运行时回存入硬盘中,所以游戏非正常结束时会丢失数据
PlayerPrefs.Save(); // 调用Save方法存储到硬盘中

读取

c 复制代码
// 不存在时返回默认值
int myInt = PlayerPrefs.GetInt("myInt");
int myInt2 = PlayerPrefs.GetInt("myInt", 100); // 参数:(key, 找不到将会返回的默认值)
// 判断是否存在
bool f = PlayerPrefs.HasKey("myInt");

删除

c 复制代码
PlayerPrefs.DeleteKey("myInt");
PlayerPrefs.DeleteAll(); // 删除所有

Type补充

父子关系

c 复制代码
// 判断某个类型是否能为自己分配空间(即父类)
Type fatherType = typeof(Father); // 获得父类Type
Type sonType = typeof(Son); // 获得子类Type
if (fatherType.IsAssignableFrom(sonType)) 	
{
    print("yes");    
}

通过反射获得泛型类型

c 复制代码
List<string, int> list = new List<string, int>();
// 获得Type
Type typeList = list.GetType();
Type[] types = typeList.GetGenericArguments(); // 获得泛型类型,返回值是type数组(泛型类型可能不止一个)
相关推荐
白山编程大哥2 分钟前
C语言篇:语法进阶
c语言·数据结构·算法
用户788477316344 分钟前
门店核销闭环:小程序扫码 + .NET 幂等接口 + WPF 看板,一天能跑通
微信小程序·c#
Logic1011 小时前
C语言/数据结构动态规划题解:爬楼梯(含坏台阶)——避开障碍物的路径计数
c语言·数据结构·动态规划·时间复杂度·空间复杂度·算法题·爬楼梯
All for pursuit.2 小时前
【回溯-6】79.单词搜索
数据结构·c++·算法·leetcode
Benny_Tang3 小时前
AT_arc180_d [ARC180D] Division into 3 题解
数据结构·c++·算法
殷色玫瑰3 小时前
C++ STL:stack、queue、priority_queue 与容器适配器详解
java·开发语言·数据结构·c++·算法·visualstudio
samble3 小时前
从零搭建灌装监控系统(十三):报警系统设计,生命周期与通知
c#·wpf·mvvm·modbus·工业控制
秋名RG5 小时前
时间复杂度、空间复杂度与算法稳定性详解
数据结构·算法·排序算法
白色的北极熊5 小时前
c语言 求余运算符
c语言·数据结构·算法
by209995 小时前
list 的使用:把节点、位置和操作联系起来的综合叙述(上)
数据结构·c++·笔记·list