概述
在游戏中存档就是把当前的游戏数据用一个Save(类)实例记录下来,然后用序列化将其保存成文本文件。当需要加载存档时用反序列化读取文本文件,将结果赋值给一个Save(类)实例达到读取存档的目的。以下示例三种方式,1、二进制 2、JSON 3、XML
小注!
Time.timeScale = 0;//游戏暂停
Time.timeScale = 1;//游戏启动
Cursor.visible = true;//鼠标可见
Cursor.visible = false;//鼠标不可见
创建Save类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]//需要标明可序列化特性
public class Save {
public List<int> livingTargetPosiont = new List<int>();
public List<int> livingMonsterPosiont = new List<int>();
public int shootNum = 0;
public int score = 0;
}
保存存档ByBinary
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;//一定要引入此命名空间!!!
using System.IO;
using UnityEngine.UI;
public void saveGameByBinary(){
//创建一个需要保存的类
Save save = creatSave();//类中需要声明序列化特性
//创建一个字节对象
BinaryFormatter by = new BinaryFormatter();
//为字节流对象的序列化准备文件流(即保存路径)
FileStream fs = File.Create(Application.dataPath + "./StreamFile"+ "./binarybin" );
//字节流序列化
by.Serialize(fs,save);
//关闭流文件
fs.Close();
if(File.Exists(Application.dataPath + "./StreamFile"+ "./binarybin" )){
StartCoroutine(showMassage("保存成功"));
}
}
读取存档ByBinary
public void loadGameByBinary(){
if(File.Exists(Application.dataPath + "./StreamFile"+ "./binarybin")){
FileStream fs = File.Open(Application.dataPath + "./StreamFile"+ "./binarybin" ,FileMode.Open);
BinaryFormatter by = new BinaryFormatter();
save = (Save)by.Deserialize(fs);
/* unSerializeSave();//反序列化操作
}
else{
StartCoroutine(showMassage("存档不存在"));
}
UnPause();
*/
}
保存存档ByJSON
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.UI;
using LitJson; //需要将此文件添加进脚本文件目录,将在资源中上传
//using System.Xml;
public void saveGameaByJson(){
//创建一个需要保存的类
Save save = creatSave();//类中需要声明序列化特性
//创建一个路径名
string filePath = Application.dataPath + "./StreamFile"+ "./binaryJSON";
//创建一个接受接受save转换成字节的字符串对象
string saveJsonStr = JsonMapper.ToJson(save);
//创建一个写入流
StreamWriter sw = new StreamWriter(filePath);
//将save写入文件
sw.Write(saveJsonStr);
关闭流
sw.Close();
if(File.Exists(Application.dataPath + "./StreamFile"+ "./binaryJSON" )){
StartCoroutine(showMassage("保存成功"));
}
}
读取存档ByJSON
public void loadGameByJson(){
if(File.Exists(Application.dataPath + "./StreamFile"+ "./binaryJSON")){
string fileStr = Application.dataPath + "./StreamFile"+ "./binaryJSON";
StreamReader sr = new StreamReader(fileStr);//文件读取流,读取之前的存档
string strJson = sr.ReadToEnd();//文件转字符串
save = JsonMapper.ToObject<Save>(strJson);//转save类!!!
//unSerializeSave();//反序列化操作
}
/*else{
StartCoroutine(showMassage("存档不存在"));
}
UnPause();
*/
}
保存存档ByXML
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.UI;
//using LitJson;
using System.Xml;
public void saveGameaByXml(){
Save save = creatSave();
//创建XML文件的存储路径
string filePath = Application.dataPath + "./StreamFile"+ "./binaryXml";
//创建XML文档
XmlDocument xmlDoc = new XmlDocument();
//创建根节点,即最上层节点
XmlElement root = xmlDoc.CreateElement("save");
//设置根节点的值
root.SetAttribute("name", "saveFile1");
//创建xml
XmlElement target;
XmlElement targetposiont;
XmlElement monsterType;
//遍历save中存储的数据,将数据转换成xml格式
for(int i = 0; i < save.livingTargetPosiont.Count; i++){
target = xmlDoc.CreateElement("target");
targetposiont = xmlDoc.CreateElement("targetpostion");
targetposiont.InnerText = save.livingTargetPosiont[i].ToString();
monsterType = xmlDoc.CreateElement("monstertype");
monsterType.InnerText = save.livingMonsterPosiont[i].ToString();
target.AppendChild(targetposiont);
target.AppendChild(monsterType);
root.AppendChild(target);
}
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);//注意!!!!XmlDocument只能添加一个XmlElement?
xmlDoc.Save(filePath);
if(File.Exists(Application.dataPath + "./StreamFile"+ "./binaryXml")){
StartCoroutine(showMassage("保存成功"));
}
}
读取存档ByXML
public void loadGameByXml(){
string filePath = Application.dataPath + "./StreamFile"+ "./binaryXml";
if(File.Exists(filePath)){
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);//文件转XML对象,之后遍历此对象得到存档的游戏数据
save = new Save();
XmlNodeList targets = xmlDoc.GetElementsByTagName("target");
if(targets.Count != 0){
foreach(XmlNode i in targets){
XmlNode targetPostion = i.ChildNodes[0];
int targetPostionIndex = int.Parse(targetPostion.InnerText);
save.livingTargetPosiont.Add(targetPostionIndex);
XmlNode monsterType = i.ChildNodes[1];
int monsterIndex = int.Parse(monsterType.InnerText);
save.livingMonsterPosiont.Add(monsterIndex);
}
}
XmlNodeList shootNUM = xmlDoc.GetElementsByTagName("shootNum");
save.shootNum = int.Parse(shootNUM[0].InnerText);
XmlNodeList scoreNUM = xmlDoc.GetElementsByTagName("score");
save.score = int.Parse(scoreNUM[0].InnerText);
//unSerializeSave();//反序列化操作
}
/*
else{
StartCoroutine(showMassage("存档不存在"));
}
UnPause();
*/
}