数据持久化 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数组(泛型类型可能不止一个)
相关推荐
习明然8 小时前
我的本地化AI项目(三)
人工智能·python·electron·c#·avalonia
罗超驿9 小时前
2.算法效率的核心密码:时间复杂度和空间复杂度详解
java·数据结构·算法
:-)10 小时前
算法-堆排序
数据结构·算法·排序算法
j7~12 小时前
【数据结构初阶】顺序表增删查改代码实现--详解
数据结构·顺序表·动态顺序表·静态顺序表
枕星而眠13 小时前
【数据结构】红黑树入门指南
运维·数据结构·c++·后端
:-)15 小时前
基础算法-选择排序
数据结构·算法·排序算法
粘稠的浆糊15 小时前
[AtCoder - abc465_d ]X to Y题解
数据结构·c++·算法
海清河晏11115 小时前
数据结构 | 二叉平衡搜索树
开发语言·数据结构·visual studio
范小多15 小时前
C#获取和风天气数据
java·开发语言·python·c#
z落落15 小时前
C# Task WaitAll、WaitAny、WhenAll、WhenAny
开发语言·c#