下面介绍几种 Unity本地记录存储的实现方式。

第一种 Unity自身提供的 PlayerPrefs

//保存数据


  1. PlayerPrefs.SetString("Name",mName);
  2. PlayerPrefs.SetInt("Age",mAge);
  3. PlayerPrefs.SetFloat("Grade",mGrade)

//读取数据


  1. mName=PlayerPrefs.GetString("Name","DefaultValue");
  2. mAge=PlayerPrefs.GetInt("Age",0);
  3. mGrade=PlayerPrefs.GetFloat("Grade",0F);

//清除所有记录

 PlayerPrefs.DeleteAll();

//删除其中某一条记录

PlayerPrefs.DeleteKey("Age");

//将记录写入磁盘

PlayerPrefs.Save()

第二种 BinaryFormatter 二进制序列化

假设有一个Player类


  1. [System. Serializable]
  2. public class Player
  3. {
  4. public int health;
  5. public int power;
  6. public Vector3 position;
  7. }

由于BinaryFormatter序列化不支持Unity的Vector3类型,所以我们需要做一下包装。


  1. public class PlayerData{
  2. public int level;
  3. public int health;
  4. public float[] position;
  5. public PlayerData(Player player)
  6. {
  7. this.level = player.level;
  8. this.health = player.health;
  9. this.position = new float[3];
  10. this.position[0] = player.transform.position.x;
  11. this.position[1] = player.transform.position.y;
  12. this.position[2] = player.transform.position.z;
  13. }
  14. }

我们对PlayerData进行保存和读取。读取出来的PlayerData可以赋给Player。


  1. public static class SaveSystem{
  2. //保存数据
  3. public static void SavePlayer(Player player)
  4. {
  5. BinaryFormatter formatter = new BinaryFormatter();
  6. string path = Application.persistentDataPath+"/player.fun";
  7. FileStream stream = new FileStream(path,FileMode.Create);
  8. PlayerData data = new PlayerData(player);
  9. formatter.Serialize(stream,data);
  10. stream.Close();
  11. }
  12. //读取数据
  13. public static PlayerData LoadPlayer()
  14. {
  15. string path = Application.persistentDataPath+"/player.fun";
  16. if(File.Exists(path))
  17. {
  18. BinaryFormatter formatter = new BinaryFormatter();
  19. FileStream stream = new FileStream(path,FileMode.Open);
  20. PlayerData data = formatter.Deserialize(stream) as PlayerData;
  21. stream.Close();
  22. return data;
  23. }else{
  24. Debug.LogError("Save file not found in "+path);
  25. return null;
  26. }
  27. }
  28. }

第三种 保存为json格式的文本文件

使用 Unity 自身API JsonUtility。

保存数据


  1. public static void SavePlayerJson(Player player)
  2. {
  3. string path = Application.persistentDataPath+"/player.json";
  4. var content = JsonUtility.ToJson(player,true);
  5. File.WriteAllText(path,content);
  6. }

读取数据


  1. public static PlayerData LoadPlayerJson()
  2. {
  3. string path = Application.persistentDataPath+"/player.json";
  4. if(File.Exists(path)){
  5. var content = File.ReadAllText(path);
  6. var playerData = JsonUtility.FromJson<PlayerData>(content);
  7. return playerData;
  8. }else{
  9. Debug.LogError("Save file not found in "+path);
  10. return null;
  11. }
  12. }

第四种 XmlSerializer进行串行化

假如有类


  1. public class Entity
  2. {
  3. public Entity()
  4. {
  5. }
  6. public Entity(string c, string f)
  7. {
  8. name = c;
  9. school = f;
  10. }
  11. public string name;
  12. public string school;
  13. }

读取数据


  1. List<Entity> entityList=null;
  2. XmlSerializer xs = new XmlSerializer(typeof(List<Entity>));
  3. using (StreamReader sr = new StreamReader(configPath))
  4. {
  5. entityList = xs.Deserialize(sr) as List<Entity>;
  6. }

保存数据


  1. List<Entity> entityList=null;
  2. XmlSerializer xs = new XmlSerializer(typeof(List<Entity>));
  3. using (StreamWriter sw = File.CreateText(configPath))
  4. {
  5. xs.Serialize(sw, entityList);
  6. }

对应的xml文件为:


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ArrayOfEntity xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  3. <Entity>
  4. <Name>Alice</Name>
  5. <School>SJTU</School>
  6. </Entity>
  7. <Entity>
  8. <Name>Cici</Name>
  9. <School>CSU</School>
  10. </Entity>
  11. <Entity>
  12. <Name>Zero</Name>
  13. <School>HIT</School>
  14. </Entity>
  15. </ArrayOfEntity>

最新文章

  1. 关于javascript的运动教程
  2. sh 测试网段在线主机
  3. 使用ycsb测试cassandra
  4. mysql where执行顺序
  5. ok6410 android driver(12)
  6. 通过表达式、函数给React组件属性赋值
  7. Condition-线程通信更高效的方式
  8. 完整的站内搜索Demo(Lucene.Net+盘古分词)
  9. 第2个Wiindows程序讲解
  10. 到处是坑的微信公众号支付开发(java)
  11. 遍历map的几种方式
  12. [JLOI2015]装备购买
  13. OpenStack-Keystone(2)
  14. JS的javascript:void(0)用法
  15. anaconda安装Opencv报错:Could NOT find PythonLibs: Found unsuitable version &quot;2.7.6&quot;,
  16. SpringMVC学习(三)———— springmvc的数据校验的实现
  17. vee-validate表单验证组件
  18. 饿了么element UI&lt;el-dialog&gt;弹出层&lt;/el-dialog&gt;修改默认样式不能在&lt;style scoped&gt;修改
  19. VS2015+MySql+EF6采坑经验总结
  20. FileInputStram入门

热门文章

  1. [c++] 分号的使用
  2. 笔记本用HDMI转VGA 使用双屏办公 听语音
  3. MySQL报错ERROR 1436 (HY000): Thread stack overrun:
  4. Ubuntu 软件更新 系统升级
  5. Linux服务之DHCP服务篇(scp)
  6. dd命令详解-(转自dkcndk)
  7. 04丨MongoDB特色及优势
  8. Python数模笔记-PuLP库(1)线性规划入门
  9. [leetcode] 72. 编辑距离(二维动态规划)
  10. linux ( crontab 定时任务命令)