游戏存档

参考:https://blog.csdn.net/y1196645376/article/details/52541882

//需要序列化的数据存储类

[System.Serializable]
public class Save 

二进制的存档方法

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

 public void SaveByBin()
    {
        //序列化过程 (save对象转换为字节流)

        //创建游戏对象并保存在save中
        Save save = CreateSaveGo();
        //创建一个二进制格式化程序
        BinaryFormatter bf = new BinaryFormatter();
        //创建一个文件流
        FileStream fileStream = File.Create(Application.dataPath + "/StreamingFile" + "/byBin.txt");
        //用二进制格式化程序来序列化save对象,参数:创建的文件流和需要序列化的对象
        bf.Serialize(fileStream, save);
        //关闭流
        fileStream.Close();
    }

 public void LoadByBin()
    {
        if (File.Exists(Application.dataPath + "/StreamingFile" + "/byBin.txt"))
        {
            //反序列化过程
            //创建一个二进制格式化程序
            BinaryFormatter bf = new BinaryFormatter();
            //打开一个文件流
            FileStream fileStream = File.Open(Application.dataPath + "/StreamingFile" + "/byBin.txt", FileMode.Open);
            //调用程序的反序列化方法,将文件流转化为save对象
            Save save = (Save)bf.Deserialize(fileStream);
            //关闭文件流
            fileStream.Close();
            //解码
           // SetGame(save); UIManager.uiManager.ShowMessage("加载成功");
        }
        else
            UIManager.uiManager.ShowMessage("文件不存在");
    }

使用json保存,这个方法需要引入LitJson 库文件

 #region json方法存档和读档
    public void SaveByJson()
    {
        Save save = CreateSaveGo();
        string filePath = Application.dataPath + "/StreamingFile" + "/byJson.json";
        //将save对象转换为json类型的字符串
        string saveJsonStr = JsonMapper.ToJson(save);
        //将字符串写入到文件中
        //创建一个streamWriter
        StreamWriter sw = new StreamWriter(filePath);
        //并写入
        sw.Write(saveJsonStr);
        //关闭
        sw.Close();
        UIManager.uiManager.ShowMessage("保存成功");
    }

  public void LoadByJson()
    {
        string filePath= Application.dataPath + "/StreamingFile" + "/byJson.json";
        if (File.Exists(filePath))
        {
            //读取流
            StreamReader sr = new StreamReader(filePath);
            //将读取到的流流赋值给jsonStr
            string jsonStr = sr.ReadToEnd();
            sr.Close();
            //将jsonStr转化为save对象
            Save save = JsonMapper.ToObject<Save>(jsonStr);
            //将读档信息转化为游戏状态
            SetGame(save);
        }else
        {
            UIManager.uiManager.ShowMessage("存档文件不存在");
        }
    }

XML方法存储游戏

创建xml文件,创建xml元素,可以设置元素的值比如名字,附加结点。结点可以设置结点中的值InnerText.

using System.Xml;

#region xml方法存档和读档
    public void SaveByXml()
    {
        Save save = CreateSaveGo();
        string filePath = Application.dataPath + "/StreamingFile" + "/byXLM.txt";
        //创建XML文档
        XmlDocument xmlDoc = new XmlDocument();
        //创建根节点 最上层结点
        XmlElement root = xmlDoc.CreateElement("save");
        //设置根节点中的值
        root.SetAttribute("name","saveFile1");
        XmlElement target;
        XmlElement targetPosition;
        XmlElement monsterType;
        //遍历save,将save对象中的数据写入xml文件中
        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.livingMonsterType[i].ToString();
            //附加至根节点,设置层级关系
            target.AppendChild(targetPosition);
            target.AppendChild(monsterType);
            root.AppendChild(target);
        }
        //定义与赋值
        XmlElement shootNum = xmlDoc.CreateElement("shootNum");
        shootNum.InnerText = save.shootNum.ToString();
        XmlElement score = xmlDoc.CreateElement("score");
        score.InnerText = save.score.ToString();
        //添加到根节点中
        root.AppendChild(shootNum);
        root.AppendChild(score);
        //根节点附加到xml文件中
        //文件层级关系 xmlDoc-root-(shootNum,score,target-(targetPosition,monsterType))
        xmlDoc.AppendChild(root);
        //保存xml文件到指定路劲
        xmlDoc.Save(filePath);
        if (File.Exists(filePath))
        {
            UIManager.uiManager.ShowMessage("保存成功");
        }
    }

 public void LoadByXml()
    {
        string filePath= Application.dataPath + "/StreamingFile" + "/byXLM.txt";
        if (File.Exists(filePath))
        {
            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.livingTargetPositions.Add(targetpositionIndex);

                    XmlNode monsterType = target.ChildNodes[1];
                    int monsterTypeIndex = int.Parse(monsterType.InnerText);
                    save.livingMonsterType.Add(monsterTypeIndex);
                }
            }
            //只能获取到xmlNodeList中
            XmlNodeList shootNumNodes = xmlDoc.GetElementsByTagName("shootNum");
            int shootNum = int.Parse(shootNumNodes[0].InnerText);
            save.shootNum = shootNum;

            XmlNodeList scoreNodes = xmlDoc.GetElementsByTagName("score");
            int score = int.Parse(scoreNodes[0].InnerText);
            save.score = score;
            SetGame(save);
            UIManager.uiManager.ShowMessage("");
        }
        else
        {
            UIManager.uiManager.ShowMessage("存档文件不存在");
        }
    }

猜你喜欢

转载自blog.csdn.net/wk6sae88/article/details/81840590