Unity本地持久化类Playerprefs
PlayerPrefs是啥
PlayerPrefs是Unity3d提供了一个用于数据本地持久化保存与读取的类。就是你退出游戏杀掉进程后,还会存在的数据;也是你卸载游戏后不删除相关的本地文件,重新下载游戏后还会存在的数据。工作原理是以key-value的形式将数据保存在本地,然后在代码中可以写入、读取、更新数据。
PlayerPrefs有啥用
可用于存储一些非关键性的数据,比如游戏存档、分数排名等等等。为什么只能保存非关键性的数据呢?因为PlayerPrefs 的最大的缺点是安全性不太高,因为它易于被访问,容易被人修改文件!另一方面,它位于系统文件夹下(如“文档”、“桌面”),所以很有可能会被意外删除咯
PlayerPrefs咋用
PlayerPrefs 只需要几行代码便可完成应用,但仅支持浮点数(Float)、整数(Int)和字符串(String)类型的值,从而导致难以处理大体量复杂对象的序列化
//Save data PlayerPrefs.SetInt("intKey",1004); PlayerPrefs.SetFloat("floatKey",10.04f); PlayerPrefs.SetString("stringKey","October 4th"); //Get data int intValue = PlayerPrefs.GetInt("intKey"); float floatValue = PlayerPrefs.GetFloat("floatKey"); string stringValue = PlayerPrefs.GetString("strKey"); //Delete all data PlayerPrefs.DeleteAll(); //Delete a data with a specific key PlayerPrefs.DeleteKey("intKey"); //Check a data with a specific key bool exist = PlayerPrefs.HasKey("intKey")
所有 PlayerPrefs 会被 Unity 程序存储在一个文件中,而存档或云存档需要我们在多个路径上储存、接收数据,所以它并不适用于处理多个存档。
Reference:
- https://blog.csdn.net/qq_38721111/article/details/103561580