using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Xml; using UnityEngine; namespace HxGame.Data { public class Region { public int indexX; public int indexY; public string regionName; } [Serializable] public class MapRegions { public int regionRowNum; public int regionColNum; public float regionWidth; public float regionHeight; public float cellWidthPixel; public float cellHeightPixel; public Region[,] regions; public void SaveXML(int mapId) { string path = PathUtil.GetXmlPath(mapId, "MapRegions"); string tmp = path.Substring(0, path.LastIndexOf('/')); if (!Directory.Exists(tmp)) Directory.CreateDirectory(tmp); if (File.Exists(path)) File.Delete(path); XmlDocument xml = new XmlDocument(); XmlDeclaration node = xml.CreateXmlDeclaration("1.0", "utf-8", "no"); xml.AppendChild(node); XmlElement Root = xml.CreateElement("config"); xml.AppendChild(Root); XmlElement Settings = xml.CreateElement("Settings");//节点Settings xml.DocumentElement.AppendChild(Settings); SaveMapInfoConfig(mapId, Settings); XmlElement Regions = xml.CreateElement("Regions");//节点Settings xml.DocumentElement.AppendChild(Regions); SaveRegions(xml, Regions); xml.Save(path); UIWindow.Instance.ShowMessage("保存地图配置成功"); } private void SaveMapInfoConfig(int mapId, XmlElement xmlSettings) { xmlSettings.SetAttribute("mapID", mapId.ToString()); xmlSettings.SetAttribute("regionRowNum", regionRowNum.ToString()); xmlSettings.SetAttribute("regionColNum", regionColNum.ToString()); xmlSettings.SetAttribute("regionWidth", regionWidth.ToString()); xmlSettings.SetAttribute("regionHeight", regionHeight.ToString()); xmlSettings.SetAttribute("cellWidthPixel", cellWidthPixel.ToString()); xmlSettings.SetAttribute("cellHeightPixel", cellHeightPixel.ToString()); } private void SaveRegions(XmlDocument xml, XmlElement xmlRegions) { XmlElement child = null; foreach(var region in regions) { child = xml.CreateElement("Region"); child.SetAttribute("IndexX", region.indexX.ToString()); child.SetAttribute("IndexY", region.indexY.ToString()); child.SetAttribute("regionName", region.regionName); xmlRegions.AppendChild(child); } } public bool LoadXML(int mapId) { string path = PathUtil.GetXmlPath(mapId, "MapRegions"); if (!File.Exists(path)) { UIWindow.Instance.ShowMessage("没有找到地形配置文件"); return false; } XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(path); XmlNode xmlRoot = xmlDocument.SelectSingleNode("config"); XmlNode xmlSettings = xmlRoot.SelectSingleNode("Settings"); XmlNode xmlRegions = xmlRoot.SelectSingleNode("Regions"); int id = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("mapID").Value); if (id != mapId) { UIWindow.Instance.ShowMessage("地图ID错误,请检查MapRegions配置文件"); return false; } LoadMapInfoConfig(xmlSettings); LoadRegions(xmlRegions); return true; } private void LoadMapInfoConfig(XmlNode xmlSettings) { regionRowNum = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("regionRowNum").Value); regionColNum = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("regionColNum").Value); regionWidth = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("regionWidth").Value); regionHeight = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("regionHeight").Value); cellWidthPixel = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("cellWidthPixel").Value); cellHeightPixel = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("cellHeightPixel").Value); } private void LoadRegions(XmlNode xmlRegions) { XmlNode xmlNode = null; XmlNodeList xmlNodeList = xmlRegions.ChildNodes; regions = new Region[regionRowNum, regionColNum]; for (int i = 0; i < xmlNodeList.Count; i++) { xmlNode = xmlNodeList.Item(i); if (xmlNode.Name != "Region") continue; Region r = new Region(); r.indexX = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("IndexX").Value); r.indexY = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("IndexY").Value); r.regionName = xmlNode.Attributes.GetNamedItem("regionName").Value; regions[r.indexX, r.indexY] = r; } } } }