Files
HX_MapEditor/Assets/Scripts/System/MapRegions.cs

144 lines
5.2 KiB
C#
Raw Normal View History

2025-06-14 13:46:24 +08:00
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
{
2025-06-16 23:59:28 +08:00
public int regionRowNum;
public int regionColNum;
public float regionWidth;
public float regionHeight;
public float cellWidthPixel;
public float cellHeightPixel;
2025-06-14 13:46:24 +08:00
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");//<2F>ڵ<EFBFBD>Settings
xml.DocumentElement.AppendChild(Settings);
SaveMapInfoConfig(mapId, Settings);
XmlElement Regions = xml.CreateElement("Regions");//<2F>ڵ<EFBFBD>Settings
xml.DocumentElement.AppendChild(Regions);
SaveRegions(xml, Regions);
xml.Save(path);
UIWindow.Instance.ShowMessage("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD>óɹ<C3B3>");
}
private void SaveMapInfoConfig(int mapId, XmlElement xmlSettings)
{
xmlSettings.SetAttribute("mapID", mapId.ToString());
2025-06-16 23:59:28 +08:00
xmlSettings.SetAttribute("regionRowNum", regionRowNum.ToString());
xmlSettings.SetAttribute("regionColNum", regionColNum.ToString());
2025-06-14 13:46:24 +08:00
xmlSettings.SetAttribute("regionWidth", regionWidth.ToString());
xmlSettings.SetAttribute("regionHeight", regionHeight.ToString());
2025-06-16 23:59:28 +08:00
xmlSettings.SetAttribute("cellWidthPixel", cellWidthPixel.ToString());
xmlSettings.SetAttribute("cellHeightPixel", cellHeightPixel.ToString());
2025-06-14 13:46:24 +08:00
}
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(<><C3BB><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>");
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("<22><>ͼID<49><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>MapRegions<6E><73><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>");
return false;
}
LoadMapInfoConfig(xmlSettings);
LoadRegions(xmlRegions);
return true;
}
private void LoadMapInfoConfig(XmlNode xmlSettings)
{
2025-06-16 23:59:28 +08:00
regionRowNum = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("regionRowNum").Value);
regionColNum = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("regionColNum").Value);
2025-06-14 13:46:24 +08:00
regionWidth = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("regionWidth").Value);
2025-06-16 23:59:28 +08:00
regionHeight = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("regionHeight").Value);
cellWidthPixel = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("cellWidthPixel").Value);
cellHeightPixel = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("cellHeightPixel").Value);
2025-06-14 13:46:24 +08:00
}
private void LoadRegions(XmlNode xmlRegions)
{
XmlNode xmlNode = null;
XmlNodeList xmlNodeList = xmlRegions.ChildNodes;
2025-06-16 23:59:28 +08:00
regions = new Region[regionRowNum, regionColNum];
2025-06-14 13:46:24 +08:00
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;
}
}
}
}