This commit is contained in:
2025-06-16 23:59:28 +08:00
parent 60fe962606
commit 1a5115c678
10 changed files with 260 additions and 156 deletions

View File

@@ -17,12 +17,18 @@ using UnityEngine.Rendering;
using UnityEngine.UI;
using UnityEngine.UIElements;
using static MapManager;
using static UnityEngine.Rendering.DebugUI.Table;
public partial class MapManager : MonoBehaviour
{
[SerializeField]
private Transform mapRegion;
public Dictionary<string, (int maxRow, int maxCol)> allMaps = new Dictionary<string, (int maxRow, int maxCol)>();
public Dictionary<string, (int maxRow, int maxCol)> allMaps = new Dictionary<string, (int maxRow, int maxCol)>();
/// <summary>
/// 当前地图区域
/// </summary>
private MapRegions _curMapRegions;
public bool LoadObsXml()
{
if (_curOpenMapId < 0)
@@ -90,53 +96,44 @@ public partial class MapManager : MonoBehaviour
}
return true;
}
public void LoadRegionXML(int mapId)
/// <summary>
/// 保存地图配置
/// </summary>
/// <param name="mapId"></param>
public void SaveRegionXML()
{
Transform regionsTrans = UIWindow.Instance.mapTrans.Find("Regions");
if (regionsTrans == null)
int mapId = _curOpenMapId;
if (!allMaps.TryGetValue(_curOpenMapId.ToString(), out var curMap))
{
UIWindow.Instance.ShowMessage("没有找到Regions节点");
UIWindow.Instance.ShowMessage($"没有扫描到当前地图 [{_curOpenMapId}]");
return;
}
MapRegions mapRegions = new MapRegions();
mapRegions.mapWidth = Convert.ToInt32(txtMapWidth.text);
mapRegions.mapHeight = Convert.ToInt32(txtMapHeight.text);
mapRegions.regionWidth = Convert.ToInt32(txtRegionWidth.text);
mapRegions.regionHeight = Convert.ToInt32(txtRegionHeight.text);
int row = mapRegions.mapWidth / MapManager.CELLSCALE / mapRegions.regionWidth;
int col = mapRegions.mapHeight / MapManager.CELLSCALE / mapRegions.regionHeight;
Transform trans = null;
mapRegions.regions = new Region[row, col];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
mapRegions.regions[i, j] = new Region();
mapRegions.regions[i, j].indexX = i;
mapRegions.regions[i, j].indexY = j;
string regionName = $"{i}_{j}";
trans = regionsTrans.Find(regionName);
if (trans == null)
{
UIWindow.Instance.ShowMessage($"没有找到region节点 name={regionName}");
return;
}
//mapRegions.regions[i, j].regionName = trans.GetComponent<Image>().sprite.name;
mapRegions.regions[i, j].regionName = trans.GetComponent<RawImage>().texture.name;
}
}
mapRegions.SaveXML(mapId);
_curMapRegions?.SaveXML(mapId);
//MapRegions mapRegions = new MapRegions();
//mapRegions.regionRowNum = curMap.maxRow;
//mapRegions.regionColNum = curMap.maxCol;
//mapRegions.regionWidth = PicMapPixel;
//mapRegions.regionHeight = PicMapPixel;
//mapRegions.cellWidthPixel = widthPixel;
//mapRegions.cellHeightPixel = heightPixel;
//mapRegions.regions = new Region[mapRegions.regionRowNum, mapRegions.regionColNum];
//for (int row = 0; row < mapRegions.regionRowNum; row++)
//{
// for (int col = 0; col < mapRegions.regionColNum; col++)
// {
// mapRegions.regions[row, col] = new Region();
// mapRegions.regions[row, col].indexX = row;
// mapRegions.regions[row, col].indexY = col;
// int ldx = mapRegions.regionRowNum - row;
// int ldy = col + 1;
// mapRegions.regions[row, col].regionName = $"v{mapId}_r{ldx}_c{ldy}"; // 构造文件名v1000_r1_c1.jpg
// }
//}
//mapRegions.SaveXML(mapId);
}
public void LoadMapRegions(int mapId)
public void LoadMapRegionSprites(int mapId)
{
_curOpenMapId = mapId;
if (!allMaps.TryGetValue(mapId.ToString(),out var mapInfo))
{
UIWindow.Instance.ShowMessage("µØÍ¼²»´æÔÚ¡£¡£¡£");
@@ -144,10 +141,16 @@ public partial class MapManager : MonoBehaviour
}
int mapRownum = mapInfo.maxRow;
int mapColumn = mapInfo.maxCol;
//平铺;
float jpgscenew = 512f / 100;
MapManager.Instance.ReseCamera(jpgscenew * mapColumn, jpgscenew * mapRownum);
MapManager.Instance.GenerateMap(jpgscenew * mapColumn, jpgscenew * mapRownum, 0.48f, 0.32f);
_curMapRegions = new MapRegions();
_curOpenMapId = mapId;
_curMapRegions.regionRowNum = mapRownum;
_curMapRegions.regionColNum = mapColumn;
_curMapRegions.regionWidth = PicMapPixel;
_curMapRegions.regionHeight = PicMapPixel;
_curMapRegions.cellWidthPixel = widthPixel;
_curMapRegions.cellHeightPixel = heightPixel;
_curMapRegions.regions = new Region[mapRownum, mapColumn];
float jpgscenew = PicMapPixel / 100;
for (int row = 0; row < mapRownum; row++)
{
for (int col = 0; col < mapColumn; col++)
@@ -160,9 +163,18 @@ public partial class MapManager : MonoBehaviour
float x = col * jpgscenew;
float y = (mapRownum - row - 1) * jpgscenew;
obj.transform.position = new Vector2(x, y);
multithreadLoadTextrue(spPath,sr);
}
_curMapRegions.regions[row, col] = new Region();
_curMapRegions.regions[row, col].indexX = row;
_curMapRegions.regions[row, col].indexY = col;
multithreadLoadTextrue(spPath, sr);
int ldx = mapRownum - row;
int ldy = col + 1;
_curMapRegions.regions[row, col].regionName = $"v{mapId}_r{ldx}_c{ldy}"; // 构造文件名v1000_r1_c1.jpg
}
}
MapManager.Instance.ReseCamera(jpgscenew * mapColumn, jpgscenew * mapRownum);
MapManager.Instance.GenerateMap(jpgscenew * mapColumn, jpgscenew * mapRownum, widthPixel / 100.0f, heightPixel / 100.0f);
}
public void ClearMapRegions()
{
@@ -244,8 +256,4 @@ public partial class MapManager : MonoBehaviour
//}
}
internal void SaveCellsXml()
{
throw new NotImplementedException();
}
}

View File

@@ -15,6 +15,12 @@ public partial class MapManager : MonoBehaviour
{
//<2F><>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public const int CELLSCALE = 100;
//<2F><><EFBFBD>ӿ<EFBFBD><D3BF><EFBFBD><EFBFBD><EFBFBD>
public const int widthPixel = 48;
//<2F><><EFBFBD>Ӹ<EFBFBD><D3B8><EFBFBD><EFBFBD><EFBFBD>
public const int heightPixel = 32;
//<2F><><EFBFBD>ŵ<EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>
public const int PicMapPixel = 512;
public delegate void LoadFinishedCallback();
public LoadFinishedCallback onLoadFinishedCallback;

View File

@@ -18,10 +18,12 @@ namespace HxGame.Data
[Serializable]
public class MapRegions
{
public int mapWidth;
public int mapHeight;
public int regionWidth;
public int regionHeight;
public int regionRowNum;
public int regionColNum;
public float regionWidth;
public float regionHeight;
public float cellWidthPixel;
public float cellHeightPixel;
public Region[,] regions;
@@ -57,10 +59,12 @@ namespace HxGame.Data
private void SaveMapInfoConfig(int mapId, XmlElement xmlSettings)
{
xmlSettings.SetAttribute("mapID", mapId.ToString());
xmlSettings.SetAttribute("mapWidth", mapWidth.ToString());
xmlSettings.SetAttribute("mapHeight", mapHeight.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)
@@ -108,20 +112,19 @@ namespace HxGame.Data
private void LoadMapInfoConfig(XmlNode xmlSettings)
{
mapWidth = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("mapWidth").Value);
mapHeight = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("mapHeight").Value);
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);
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)
{
int row = mapWidth / MapManager.CELLSCALE / regionWidth;
int col = mapHeight / MapManager.CELLSCALE / regionHeight;
XmlNode xmlNode = null;
XmlNodeList xmlNodeList = xmlRegions.ChildNodes;
regions = new Region[row, col];
regions = new Region[regionRowNum, regionColNum];
for (int i = 0; i < xmlNodeList.Count; i++)
{
xmlNode = xmlNodeList.Item(i);

View File

@@ -229,15 +229,4 @@ public class UICellEditor : MonoBehaviour
{
MapManager.Instance.CleanSafe();
}
public void SaveCells()
{
if (!UIWindow.Instance.uiCellInfo.bMapOpened)
{
UIWindow.Instance.ShowMessage("<22><><EFBFBD>ȴ򿪵<C8B4>ͼ");
return;
}
MapManager.Instance.SaveCellsXml();
}
}

View File

@@ -60,25 +60,6 @@ public class UICellInfo : MonoBehaviour
UIWindow.Instance.uiCellEditor.togEdit.isOn = false;
UIWindow.Instance.uiCellEditor.togPathTest.isOn = false;
}
public void LoadCells()
{
if (!bMapOpened)
{
UIWindow.Instance.ShowMessage("<22><><EFBFBD>ȴ򿪵<C8B4>ͼ");
return;
}
if (!MapManager.Instance.LoadObsXml())
CalculationCells();
txtCellWidth.text = MapManager.Instance.CellWidth.ToString();
txtCellHeight.text = MapManager.Instance.CellHeight.ToString();
txtCellRows.text = MapManager.Instance.CellRows.ToString();
txtCellCols.text = MapManager.Instance.CellCols.ToString();
txtTotalCells.text = (MapManager.Instance.CellRows * MapManager.Instance.CellCols).ToString();
}
public void ShowCells()
{
MapManager.Instance.ShowMapGrid();

View File

@@ -312,64 +312,14 @@ public class UICreateMap : MonoBehaviour
btnAutoCleanupRegion.gameObject.SetActive(false);
}
public void SaveRegions()
{
if (!CheckValid())
return;
Transform regionsTrans = UIWindow.Instance.mapTrans.Find("Regions");
if (regionsTrans == null)
{
UIWindow.Instance.ShowMessage(<><C3BB><EFBFBD>ҵ<EFBFBD>Regions<6E>ڵ<EFBFBD>");
return;
}
MapRegions mapRegions = new MapRegions();
mapRegions.mapWidth = Convert.ToInt32(txtMapWidth.text);
mapRegions.mapHeight = Convert.ToInt32(txtMapHeight.text);
mapRegions.regionWidth = Convert.ToInt32(txtRegionWidth.text);
mapRegions.regionHeight = Convert.ToInt32(txtRegionHeight.text);
int row = mapRegions.mapWidth / MapManager.CELLSCALE / mapRegions.regionWidth;
int col = mapRegions.mapHeight / MapManager.CELLSCALE / mapRegions.regionHeight;
Transform trans = null;
mapRegions.regions = new Region[row, col];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
mapRegions.regions[i, j] = new Region();
mapRegions.regions[i, j].indexX = i;
mapRegions.regions[i, j].indexY = j;
string regionName = $"{i}_{j}";
trans = regionsTrans.Find(regionName);
if(trans == null)
{
UIWindow.Instance.ShowMessage($"û<><C3BB><EFBFBD>ҵ<EFBFBD>region<6F>ڵ<EFBFBD> name={regionName}");
return;
}
//mapRegions.regions[i, j].regionName = trans.GetComponent<Image>().sprite.name;
mapRegions.regions[i, j].regionName = trans.GetComponent<RawImage>().texture.name;
}
}
mapRegions.SaveXML(Convert.ToInt32(txtMapID.text));
}
public void LoadMapRegions(int mapId)
{
//MapRegions mapRegions = new MapRegions();
//if (!mapRegions.LoadXML(mapId))
// return;
if (!_curMapRegions.LoadXML(mapId))
return;
txtMapID.text = mapId.ToString();
txtMapWidth.text = _curMapRegions.mapWidth.ToString();
txtMapHeight.text = _curMapRegions.mapHeight.ToString();
txtMapWidth.text = _curMapRegions.regionRowNum.ToString();
txtMapHeight.text = _curMapRegions.regionColNum.ToString();
txtRegionWidth.text = _curMapRegions.regionWidth.ToString();
txtRegionHeight.text = _curMapRegions.regionHeight.ToString();

View File

@@ -114,8 +114,7 @@ public class UIMapPanel : MonoBehaviour
}
int mapId = Convert.ToInt32(dropMap.options[dropMap.value].text);
_curOpenMapId = mapId;
MapManager.Instance.LoadMapRegions(_curOpenMapId);
UIWindow.Instance.uiCellInfo.LoadCells();
MapManager.Instance.LoadMapRegionSprites(_curOpenMapId);
//UIWindow.Instance.uiCellInfo.ShowCells();
//UIWindow.Instance.uiEditMapConfig.LoadMapConfig(mapId);
//UIWindow.Instance.uiMonstersPanel.LoadMonsterConfig(mapId);
@@ -174,15 +173,16 @@ public class UIMapPanel : MonoBehaviour
{
_saving = true;
Debug.Log("<22><><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>...");
MapManager.Instance.SaveRegionXML();
//UIWindow.Instance.uiCreateMap.SaveRegions();
UIWindow.Instance.uiCellEditor.SaveCells();
UIWindow.Instance.uiEditMapConfig.SaveMapConfig();
UIWindow.Instance.uiMonstersPanel.SaveMonsterConfig();
UIWindow.Instance.uiNpcsPanel.SaveNpcsConfig();
UIWindow.Instance.uiTriggersPanel.SaveTriggersConfig();
UIWindow.Instance.uiJuBaosPanel.SaveJuBaoConfig();
UIWindow.Instance.uiFuBensPanel.SaveFuBenConfig();
UIWindow.Instance.ShowMessage("<22><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>");
//UIWindow.Instance.uiCellEditor.SaveCells();
//UIWindow.Instance.uiEditMapConfig.SaveMapConfig();
//UIWindow.Instance.uiMonstersPanel.SaveMonsterConfig();
//UIWindow.Instance.uiNpcsPanel.SaveNpcsConfig();
//UIWindow.Instance.uiTriggersPanel.SaveTriggersConfig();
//UIWindow.Instance.uiJuBaosPanel.SaveJuBaoConfig();
//UIWindow.Instance.uiFuBensPanel.SaveFuBenConfig();
//UIWindow.Instance.ShowMessage("<22><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>");
_saving = false;
}
}