要保存的数据
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]//可序列化特性
public class Save
{
public List<int> livingTagetPositions = new List<int>();
public List<int> livingMonsterTypes = new List<int>();
public int shootNum = 0;
public int score = 0;
}
数据的保存与读取
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public class GameManager : MonoBehaviour
{
//创建Save对象并存储当前游戏状态信息
private Save CreateSaveGo()
{
//创建Save对象
Save save = new Save();
//遍历当前所有target
//如果其中有处于存活状态的怪物
//就把该target的位置信息,激活状态的怪物类型添加到List中
foreach (GameObject targetGo in targetGOs)
{
TargetManager targetManager = targetGo.GetComponent<TargetManager>();
if (targetManager.activeMonster != null)
{
save.livingTagetPositions.Add(targetManager.targetPosition);
int type = targetManager.activeMonster.GetComponent<MonsterManager>().monsterType;
save.livingMonsterTypes.Add(type);
}
}
//把shootNum和score保存在Save对象中
save.shootNum = UIManager._instance.shootNum;
save.score = UIManager._instance.score;
//返回该Save对象
return save;
}
//通过读档信息,重置游戏状态(分数、激活状态的怪物)
private void SetGame(Save save)
{
//先将所有的target里面的怪物情况,并重置所有的计时
foreach (GameObject targetGO in targetGOs)
{
targetGO.GetComponent<TargetManager>().UpdateMonsters();
}
//通过反序列化得到的Save对象中存储的信息,激活指定的怪物
for (int i = 0; i < save.livingTagetPositions.Count; i++)
{
int position = save.livingTagetPositions[i];
int type = save.livingMonsterTypes[i];
targetGOs[position].GetComponent<TargetManager>().ActiveMonsterByType(type);
}
//更新UI显示
UIManager._instance.shootNum = save.shootNum;
UIManager._instance.score = save.score;
//调整未暂停状态
UnPause();
}
//Xml:存档和读档
private void SaveByXml()
{
Save save = CreateSaveGo();
//创建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.livingTagetPositions.Count; i++)
{
//外层
target = xmlDoc.CreateElement("target");
//内层
targetPosition = xmlDoc.CreateElement("targetPosition");
//设置InnerText值
targetPosition.InnerText = save.livingTagetPositions[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);
if (File.Exists(Application.dataPath + "/StreamingFile" + "/byXml.txt"))
{
UIManager._instance.ShowMessage("保存成功!");
}
}
//Xml:存档和读档
private void LoadByXml()
{
string filePath = Application.dataPath + "/StreamingFile" + "/byXml.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中
save.livingTagetPositions.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);
UIManager._instance.ShowMessage("");
}
else
{
UIManager._instance.ShowMessage("存档文件不存在");
}
}
}
保存生成的Xml文档