数据持久化 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数组(泛型类型可能不止一个)
相关推荐
San813_LDD2 分钟前
[数据结构]LeetCode学习
数据结构·算法·图论
nnsix15 分钟前
Unity 贴图压缩格式 笔记
笔记·unity·贴图
王cb1 小时前
WinRT Server and Client c#
开发语言·c#
小欣加油1 小时前
leetcode994 腐烂的橘子
数据结构·c++·算法·leetcode·bfs
咸鱼翻身小阿橙1 小时前
在VScode使用C#并且调用opencv库
vscode·opencv·c#
Felven2 小时前
B. Fair Numbers
数据结构·算法
ysu_03143 小时前
leetcode数据结构与算法1~4
c语言·数据结构·学习·算法·leetcode
小欣加油3 小时前
leetcode2574 左右元素和的差值
数据结构·c++·算法·leetcode·职场和发展
weixin_461769403 小时前
通过数组和队列构造二叉树方法(用于算法测试),C++ vector不能直接使用null
数据结构·c++·算法·vector·nullptr·null
千寻girling3 小时前
一周没跑步了 ,今日跑步 5KM , 哑铃+健身 20min , 俯卧撑 30 个 ;
数据结构·c++·python·算法·leetcode·职场和发展·线性回归