Files
HX_MapEditor/Assets/Scripts/UI/UIMapPanel.cs
2025-06-16 21:26:36 +08:00

189 lines
5.8 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
///最上面
///功能:扫描地图、打开地图、关闭地图、放弃设置
public class UIMapPanel : MonoBehaviour
{
public Dropdown dropMap;
private int _curOpenMapId;
private bool _saving;
public int CurOpenMapId => _curOpenMapId;
// Start is called before the first frame update
void Start()
{
_saving = false;
_curOpenMapId = -1;
}
void OnDataLoaded()
{
// 获取所有地图文件夹
string mapsDirectory = Path.Combine(Application.dataPath, "GameAssets", "Maps");
if (!Directory.Exists(mapsDirectory))
{
Debug.LogError($"Maps directory not found: {mapsDirectory}");
return;
}
string[] mapFolders = Directory.GetDirectories(mapsDirectory);
foreach (string folderPath in mapFolders)
{
string[] pathSplit = folderPath.Split(Path.DirectorySeparatorChar);
string mapId = pathSplit[pathSplit.Length - 1]; // 例如 "v1000"
string textureDirectory = Path.Combine(folderPath, "Texture");
if (Directory.Exists(textureDirectory))
{
string[] imageFiles = Directory.GetFiles(textureDirectory, "*.jpg");
int maxRow = 0;
int maxCol = 0;
string pattern = $@"{mapId}_r(\d+)_c(\d+)"; // 正则表达式:匹配 v1000_rXX_cYY
Regex regex = new Regex(pattern);
foreach (string filePath in imageFiles)
{
string fileName = Path.GetFileNameWithoutExtension(filePath); // 例如 "v1000_r11_c14"
Match match = regex.Match(fileName);
if (match.Success)
{
if (int.TryParse(match.Groups[1].Value, out int row) && int.TryParse(match.Groups[2].Value, out int col))
{
maxRow = Mathf.Max(maxRow, row);
maxCol = Mathf.Max(maxCol, col);
}
}
else
{
Debug.LogWarning($"Filename {fileName} does not match expected pattern: {pattern}");
}
}
if (maxRow > 0 && maxCol > 0)
{
MapManager.Instance.allMaps[mapId] = (maxRow, maxCol);
Debug.Log($"Map {mapId} loaded with max dimensions: {maxRow}x{maxCol}");
}
else
{
Debug.LogWarning($"No valid dimensions found for map {mapId}");
}
}
else
{
Debug.LogWarning($"Texture directory not found for map {mapId}: {textureDirectory}");
}
}
}
public void ScanMap()
{
MapManager.Instance.allMaps.Clear();
OnDataLoaded();
dropMap.options.Clear();
foreach (var map in MapManager.Instance.allMaps)
{
Dropdown.OptionData od = new Dropdown.OptionData();
od.text = map.Key;
dropMap.options.Add(od);
}
dropMap.value = 1;
dropMap.value = 0; //为了默认选中第一项
}
public void OpenMap()
{
if(dropMap.options.Count == 0)
{
UIWindow.Instance.ShowMessage("请先扫描地图");
return;
}
if(_curOpenMapId > 0)
{
UIWindow.Instance.ShowMessage("请先关闭已有地图");
return;
}
int mapId = Convert.ToInt32(dropMap.options[dropMap.value].text);
_curOpenMapId = mapId;
MapManager.Instance.LoadMapRegions(_curOpenMapId);
UIWindow.Instance.uiCellInfo.LoadCells();
//UIWindow.Instance.uiCellInfo.ShowCells();
//UIWindow.Instance.uiEditMapConfig.LoadMapConfig(mapId);
//UIWindow.Instance.uiMonstersPanel.LoadMonsterConfig(mapId);
//UIWindow.Instance.uiNpcsPanel.LoadNpcsConfig(mapId);
//UIWindow.Instance.uiTriggersPanel.LoadTriggersConfig(mapId);
//UIWindow.Instance.uiFuBensPanel.LoadFuBenConfig(mapId);
//UIWindow.Instance.uiJuBaosPanel.LoadJuBaoConfig(mapId);
}
public void CloseMap()
{
MapManager.Instance.CloseMap();
_curOpenMapId = -1;
}
public bool HasMap(string mapId)
{
return MapManager.Instance.allMaps.ContainsKey(mapId);
}
public void ResetMap()
{
if (dropMap.options.Count == 0)
{
UIWindow.Instance.ShowMessage("请先扫描地图");
return;
}
if (_curOpenMapId < 0)
{
UIWindow.Instance.ShowMessage("没有地图");
return;
}
//先关闭地图
CloseMap();
UIWindow.Instance.uiEditMapConfig.Cleanup();
//再打开地图
OpenMap();
}
private void Update()
{
if (Input.GetKey(KeyCode.LeftControl) && Input.GetKey(KeyCode.S))
{
if (_saving)
return;
SaveAll();
}
}
public void SaveAll()
{
_saving = true;
Debug.Log("正在保存所有数据...");
//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("保存成功");
_saving = false;
}
}