Unity3D 本地数据持久化几种方式

下面介绍几种 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>
上一篇:Unity3D 同步加载场景


下一篇:unity3D学习3——正则表达式 图文并茂 10分钟完美通关 !!!