unity 编辑器存档_Unity开发之存档和读档的三种实现方式

此文内容源自siki学院视频,仅供学习!视频链接地址:http://www.sikiedu.com/course/129

工程使用Unity 2017.3.0f3 (64-bit)

老司机读博客,了解存档读档主体实现方式即可,仅供借鉴参考,菜鸟可以去文章结尾下载源码,或者去上面的链接直接观看视频。。。。。

首先,创建一个Save类用于保存数据

[System.Serializable]

public class Save

{

public List livingTargetPositions = new List();

public List livingMonsterTypes = new List();

public int shootNum = 0;

public int score = 0;

}

方式一:二进制方法

存档

private void SaveByBin()

{

//序列化过程(将save对象转换为字节流)

//创建save对象并保存当前游戏状态

Save save = CreateSaveGO();

//创建一个二进制格式化程序

BinaryFormatter bf = new BinaryFormatter();

//创建一个文件流

path = Application.dataPath + "/StreamingFile" + "/byBin.txt";

FileStream fileStream = File.Create(path);

//用二进制格式化程序的序列化方法来序列化Save对象,参数:创建的文件流和需要序列化的对象

bf.Serialize(fileStream, save);

//关闭流

fileStream.Close();

//即时刷新Project工程文件

AssetDatabase.Refresh();

}

读档

private void LoadByBin()

{

path = Application.dataPath + "/StreamingFile" + "/byBin.txt";

//反序列化过程

//创建一个二进制格式化程序

BinaryFormatter bf = new BinaryFormatter();

//打开一个文件流

FileStream fileStream = File.Open(path,FileMode.Open);

//调用格式化程序的反序列化方法,将文件流转换为一个save对象

Save save = bf.Deserialize(fileStream) as Save;

//关闭文件流

fileStream.Close();

}

方式二:Xml

存档

private void SaveByXml()

{

Save save = CreateSaveGO();

//创建Xml文件的存储路径

path = Application.dataPath + "/StreamingFile" + "/byXml.xml";

//创建XML文档

XmlDocument xmlDoc = new XmlDocument();

//创建根节点,即最上层节点

XmlElement root = xmlDoc.CreateElement("save");

//设置根节点中的值

root.SetAttribute("name", "saveFile1");

XmlElement target;

XmlElement targetPosition;

XmlElement monsterType;

for (int i = 0; i < save.livingTargetPositions.Count; i++)

{

target = xmlDoc.CreateElement("target");

targetPosition = xmlDoc.CreateElement("targetPosition");

//设置节点的值

targetPosition.InnerText = save.livingTargetPositions[i].ToString();

monsterType = xmlDoc.CreateElement("monsterType");

monsterType.InnerText = save.livingMonsterTypes[i].ToString();

//设置节点间的层级关系 root -- target -- (targetPosition,monsterType)

target.AppendChild(targetPosition);

target.AppendChild(monsterType);

root.AppendChild(target);

}

//设置射击数和分数节点并设置层级关系 xmlDoc -- root -- (target,shootNum,score)

XmlElement shootNum = xmlDoc.CreateElement("shootNum");

shootNum.InnerText = save.shootNum.ToString();

root.AppendChild(shootNum);

XmlElement score = xmlDoc.CreateElement("score");

score.InnerText = save.score.ToString();

root.AppendChild(score);

xmlDoc.AppendChild(root);

xmlDoc.Save(path);

AssetDatabase.Refresh();

}

读档

private void LoadByXml()

{

path = Application.dataPath + "/StreamingFile" + "/byXml.xml";Save save = new Save();

//加载XML文档

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(path);

//通过节点名称来获取元素,结果为XmlNodeList类型

XmlNodeList targets = xmlDoc.GetElementsByTagName("target");

//遍历节点所有的target节点,并获得子节点和子节点的InnerText

if (targets.Count != 0)

{

foreach (XmlNode target in targets)

{

XmlNode targetPosition = target.ChildNodes[0];

int targetPositionIndex = int.Parse(targetPosition.InnerText);

//把得到的值存储到save中

save.livingTargetPositions.Add(targetPositionIndex);

XmlNode monsterType = target.ChildNodes[1];

int monsterTypeIndex = int.Parse(monsterType.InnerText);

save.livingMonsterTypes.Add(monsterTypeIndex);

}

}

XmlNodeList shootNum = xmlDoc.GetElementsByTagName("shootNum");

int shootNumCount = int.Parse(shootNum[0].InnerText);

save.shootNum = shootNumCount;

XmlNodeList score = xmlDoc.GetElementsByTagName("score");

int scoreCount = int.Parse(score[0].InnerText);

save.score = scoreCount;

SetGame(save);

}

方式三:Json(LitJson)

存档

private void SaveByJson()

{

Save save = CreateSaveGO();

path = Application.dataPath + "/StreamingFile" + "/byJson.json";

//利用JsonMapper将save对象转换为Json格式的字符串

string saveJsonStr = JsonMapper.ToJson(save);

//将这个字符串写入到文件中

//创建一个StreamWriter,并将字符串写入

StreamWriter sw = new StreamWriter(path);

sw.Write(saveJsonStr);

//关闭写入流

sw.Close();

AssetDatabase.Refresh();

}

读档

private void LoadByJson()

{

path = Application.dataPath + "/StreamingFile" + "/byJson.json";

//创建一个StreamReader,用来读取流

StreamReader sr = new StreamReader(path);

//将读取到的流赋值给saveJsonStr

string saveJsonStr = sr.ReadToEnd();

sr.Close();

//将字符串转换为Save对象

Save save = JsonMapper.ToObject(saveJsonStr);

SetGame(save);

}

附上工程源码和LitJson库,有时间的童鞋可以去siki学院观看视频,良心推荐,真的不错!

链接:https://pan.baidu.com/s/1IOa2Dw06tMSC-hlngAk5-w 密码:glps

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值