using System; using System.Collections; using System.Collections.Generic; using System.Data.Common; using System.IO; using System.Text.RegularExpressions; using UnityEditor; using UnityEngine; using UnityEngine.UI; ///最上面 ///功能:扫描地图、打开地图、关闭地图、放弃设置 public class UIMapPanel : MonoBehaviour { public ToggleGroup mapEditorGroup; 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] = (maxCol, maxRow); Debug.Log($"Map {mapId} loaded with max dimensions: {maxCol}x{maxRow}"); } else { Debug.LogWarning($"No valid dimensions found for map {mapId}"); } } else { Debug.LogWarning($"Texture directory not found for map {mapId}: {textureDirectory}"); } } } public void ExportMap() { MapManager.Instance.SaveAtlasToFile(); } 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; UIWindow.Instance.uiEditMapConfig.LoadMapConfig(mapId); MapManager.Instance.LoadMapRegionSprites(_curOpenMapId); MapManager.Instance.LoadMapObs(_curOpenMapId); 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); //如果没有配置,需要创建阻隔点 if (MapManager.Instance.map == null) { MapManager.Instance.CreateObs(_curOpenMapId); } UICellInfo.Instance.ShowMapCellInfo(); } public void CloseMap() { MapManager.Instance.CloseMap(); _curOpenMapId = -1; foreach (var toggle in mapEditorGroup.ActiveToggles()) { toggle.isOn = false; } } public bool HasMap(string mapId) { return MapManager.Instance.allMaps.ContainsKey(mapId); } private void Update() { if (Input.GetKey(KeyCode.LeftControl) && Input.GetKey(KeyCode.S)) { if (_saving) return; SaveAll(); } } public void SaveAll() { _saving = true; Debug.Log("正在保存所有数据..."); MapManager.Instance.SaveRegionXML(); MapManager.Instance.SaveMapObs(); 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; #if UNITY_EDITOR AssetDatabase.Refresh(); #endif } }