此文内容源自siki学院视频,仅供学习!视频链接地址:http://www.sikiedu.com/course/129
工程使用Unity 2017.3.0f3 (64-bit)
老司机读博客,了解存档读档主体实现方式即可,仅供借鉴参考,菜鸟可以去文章结尾下载源码,或者去上面的链接直接观看视频。。。。。
首先,创建一个Save类用于保存数据
[System.Serializable]
public class Save
{
public List<int> livingTargetPositions = new List<int>();
public List<int> livingMonsterTypes = new List<int>();
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<Save>(saveJsonStr);
SetGame(save);
}
附上工程源码和LitJson库,有时间的童鞋可以去siki学院观看视频,良心推荐,真的不错!
链接:https://pan.baidu.com/s/1IOa2Dw06tMSC-hlngAk5-w 密码:glps