此文內容源自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