数据持久化 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数组(泛型类型可能不止一个)
相关推荐
wfserial4 分钟前
c#使用微软自带speech选择男声仍然是女声的一种原因
microsoft·c#·speech
阔皮大师2 小时前
INote轻量文本编辑器
java·javascript·python·c#
小妖6662 小时前
js 实现快速排序算法
数据结构·算法·排序算法
kylezhao20192 小时前
C# 中的 SOLID 五大设计原则
开发语言·c#
啦啦啦_99993 小时前
Redis-5-doFormatAsync()方法
数据库·redis·c#
Porco.w4 小时前
C#与三菱PLC FX5U通信
网络·c#
独好紫罗兰4 小时前
对python的再认识-基于数据结构进行-a003-列表-排序
开发语言·数据结构·python
wuhen_n5 小时前
JavaScript内置数据结构
开发语言·前端·javascript·数据结构
2401_841495645 小时前
【LeetCode刷题】二叉树的层序遍历
数据结构·python·算法·leetcode·二叉树··队列
独好紫罗兰5 小时前
对python的再认识-基于数据结构进行-a002-列表-列表推导式
开发语言·数据结构·python