using AX.MessageSystem; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Xml; using UnityEngine; public class DisasterManager : MonoBehaviour { public static DisasterInfo disaster; public static List firstNodeObjects; public static List secondNodeObjects; public static bool updateFlag; public static PlayModel model; public static bool editorRight; private void Awake() { //updateFlag = false; //editorRight = true;//单机版 //editorRight = true;//联机版 } private void Start() { /*disaster = new DisasterInfo(); disaster.Id = 111; disaster.Name = "aaa";*/ MessageDispatcher.AddListener("DisasterUpdateCompelete", ResetData);//更新完成重置数据 firstNodeObjects = new List(); secondNodeObjects = new List(); InstantiateData(); } private void OnDestroy() { MessageDispatcher.RemoveListener("DisasterUpdateCompelete", ResetData);//更新完成重置数据 } private void ResetData(IMessage obj) { InstantiateData(); } public void InstantiateData() { firstNodeObjects.Clear(); secondNodeObjects.Clear(); string filepath = GetNodeListXML(); if (!File.Exists(filepath)) { CreateNodeListXML(); } else { LoadNodeListXML(); } MessageDispatcher.SendMessage("DataInitializationComplete"); } /// /// 获取节点存储文件路径 /// /// public static string GetNodeListXML() { return Application.dataPath + @"/Data/" + disaster.Id + "/File/NodeList.xml"; } /// /// 创建节点存储文件 /// private void CreateNodeListXML() { string path = Application.dataPath + @"/Data/" + disaster.Id + "/File/"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } XmlDocument document = new XmlDocument(); XmlDeclaration xmldecl = document.CreateXmlDeclaration("1.0", "UTF-8", "yes"); document.AppendChild(xmldecl); XmlElement nodeList = document.CreateElement("NodeList"); document.AppendChild(nodeList); XmlElement firstNode = document.CreateElement("FirstNode"); nodeList.AppendChild(firstNode); XmlElement nextID = document.CreateElement("NextID"); nextID.InnerText = "0"; firstNode.AppendChild(nextID); XmlElement nodes = document.CreateElement("Nodes"); firstNode.AppendChild(nodes); XmlElement secondNode = document.CreateElement("SecondNode"); nodeList.AppendChild(secondNode); nextID = document.CreateElement("NextID"); nextID.InnerText = "0"; secondNode.AppendChild(nextID); nodes = document.CreateElement("Nodes"); secondNode.AppendChild(nodes); document.Save(GetNodeListXML()); document = null; } /// /// 读取节点文件,获取节点数据 /// private void LoadNodeListXML() { string filepath = GetNodeListXML(); if (File.Exists(filepath)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filepath); XmlNode firstNodes = xmlDoc.SelectSingleNode("NodeList/FirstNode/Nodes"); foreach (XmlElement node in firstNodes) { FirstNodeObject obj = new FirstNodeObject(); obj.nodeID = int.Parse(node.GetAttribute("ID")); obj.nodeName = node.GetAttribute("Name"); firstNodeObjects.Add(obj); } XmlNode secondNodes = xmlDoc.SelectSingleNode("NodeList/SecondNode/Nodes"); foreach (XmlElement node in secondNodes) { SecondNodeObject obj = new SecondNodeObject(); obj.nodeID = int.Parse(node.GetAttribute("ID")); obj.nodeName = node.GetAttribute("Name"); obj.nodeDetail = node.GetAttribute("Detail"); obj.ParentID = int.Parse(node.GetAttribute("ParentID")); secondNodeObjects.Add(obj); } } } }