游戏中往往需要存档和读档,进行游戏的保存和读取,游戏的存档和读档其实就是对象的序列化和反序列化。如果游戏中需要存储的类是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; }
讯享网
讯享网//二进制方式 //存储 BinaryFormatter bf = new BinaryFormatter(); //创建一个文件流 FileStream fileStream = File.Create(Application.dataPath + "/StreamingFile" + "/byBin.txt"); //用二进制格式化程序的序列化方法来序列化Save对象,参数:创建的文件流和需要序列化的对象 bf.Serialize(fileStream, save); //关闭流 fileStream.Close(); //如果文件存在,则显示保存成功 if (File.Exists(Application.dataPath + "/StreamingFile" + "/byBin.txt")) {
print("保存成功"); } //读取 BinaryFormatter bf = new BinaryFormatter(); //打开一个文件流 FileStream fileStream = File.Open(Application.dataPath + "/StreamingFile" + "/byBin.txt", FileMode.Open); //调用格式化程序的反序列化方法,将文件流转换为一个Save对象 Save save= (Data)bf.Deserialize(fileStream); //关闭文件流 fileStream.Close();
using LitJson; //Json方式 //存档 string filePath = Application.dataPath + "/StreamingFile" + "/byJson.json"; //利用JsonMapper将save对象转换为Json格式的字符串 string saveJsonStr = JsonMapper.ToJson(save); //将这个字符串写入到文件中 //创建一个StreamWriter,并将字符串写入文件中 StreamWriter sw = new StreamWriter(filePath); sw.Write(saveJsonStr); //关闭StreamWriter sw.Close(); print("保存成功"); //读档 //创建一个StreamReader,用来读取流 string filePath = Application.dataPath + "/StreamingFile" + "/byJson.json"; StreamReader sr = new StreamReader(filePath); //将读取到的流赋值给jsonStr string jsonStr = sr.ReadToEnd(); //关闭 sr.Close(); //将字符串jsonStr转换为Save对象 Save save = JsonMapper.ToObject<Save>(jsonStr);
讯享网using System.Xml; //Xml方式 //存档 string filePath = Application.dataPath + "/StreamingFile" + "/byXML.txt"; //创建XML文档 XmlDocument xmlDoc = new XmlDocument(); //创建根节点,即最上层节点 XmlElement root = xmlDoc.CreateElement("save"); //设置根节点中的值 root.SetAttribute("name", "saveFile1"); //创建XmlElement XmlElement target; XmlElement targetPosition; XmlElement monsterType; //遍历save中存储的数据,将数据转换成XML格式 for(int i = 0; i < save.livingTargetPositions.Count; i++) {
target = xmlDoc.CreateElement("target"); targetPosition = xmlDoc.CreateElement("targetPosition"); //设置InnerText值 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-- (targetPosition, monsterType), 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(filePath); //读档 string filePath = Application.dataPath + "/StreamingFile" + "/byXML.txt"; Save save = new Save(); //加载XML文档 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filePath); //通过节点名称来获取元素,结果为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;

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/118277.html