1. 程式人生 > >遊戲存檔

遊戲存檔

參考: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("存檔檔案不存在");
        }
    }