PlayerPrefs
采用键值对的形式对数据进行存储,可以保存int、float、string类型的数据。
用法如下:
// 存储
PlayerPrefs.SetInt("IntParam",1);
PlayerPrefs.SetFloat("FloatParam",1.0f);
PlayerPrefs.SetString("StringParam","123");
PlayerPrefs.Save();
// 读取
int intParam = PlayerPrefs.GetInt("IntParam");
float floatParam = PlayerPrefs.GetFloat("FloatParam");
string stringParam = PlayerPrefs.GetString("StringParam");
// 其他
bool hasIntParam = PlayerPrefs.HasKey("IntParam");
PlayerPrefs.DeleteKey("IntParam");
PlayerPrefs.DeleteAll();
需要注意的是,PlayerPrefs键值对存储的位置位于注册表中
计算机\HKEY_CURRENT_USER\Software\Unity\UnityEditor\组织名\工程名
的目录下

二进制
采用二进制的形式保存游戏数据,需要用到System.Runtime.Serialization.Formatters.Binary
命名空间下的BinaryFormatter二进制转换器。注意被序列化的类需要有 [Serializable] 标记。
private void SaveByBinary()
{
// 创建二进制转换器
BinaryFormatter bf = new BinaryFormatter();
// 创建文件流
FileStream fileStream = File.Create(Application.dataPath + "/StreamingFile/" + "saveByBi.txt");
// 将对象序列化为二进制写入文件流
bf.Serialize(fileStream,saveContent);
// 关闭文件流
fileStream.Close();

最低0.47元/天 解锁文章
463

被折叠的 条评论
为什么被折叠?



