Files
HX_MapEditor/Assets/Scripts/UI/UIMapPanel.cs

178 lines
5.6 KiB
C#
Raw Normal View History

2025-06-14 13:46:24 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2025-06-19 01:31:00 +08:00
using System.Data.Common;
2025-06-14 13:46:24 +08:00
using System.IO;
2025-06-16 00:15:41 +08:00
using System.Text.RegularExpressions;
2025-06-25 15:57:50 +08:00
using UnityEditor;
2025-06-14 13:46:24 +08:00
using UnityEngine;
using UnityEngine.UI;
///<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
///<2F><><EFBFBD>ܣ<EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD>򿪵<EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD>رյ<D8B1>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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()
{
2025-06-16 00:15:41 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>е<EFBFBD>ͼ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
string mapsDirectory = Path.Combine(Application.dataPath, "GameAssets", "Maps");
if (!Directory.Exists(mapsDirectory))
2025-06-14 13:46:24 +08:00
{
2025-06-16 00:15:41 +08:00
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]; // <20><><EFBFBD><EFBFBD> "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+)"; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>ƥ<EFBFBD><C6A5> v1000_rXX_cYY
Regex regex = new Regex(pattern);
foreach (string filePath in imageFiles)
{
string fileName = Path.GetFileNameWithoutExtension(filePath); // <20><><EFBFBD><EFBFBD> "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}");
}
2025-06-14 13:46:24 +08:00
}
}
public void ScanMap()
2025-06-16 00:15:41 +08:00
{
MapManager.Instance.allMaps.Clear();
2025-06-14 13:46:24 +08:00
OnDataLoaded();
dropMap.options.Clear();
2025-06-16 00:15:41 +08:00
foreach (var map in MapManager.Instance.allMaps)
{
2025-06-14 13:46:24 +08:00
Dropdown.OptionData od = new Dropdown.OptionData();
2025-06-16 00:15:41 +08:00
od.text = map.Key;
2025-06-14 13:46:24 +08:00
dropMap.options.Add(od);
}
dropMap.value = 1;
dropMap.value = 0; //Ϊ<><CEAA>Ĭ<EFBFBD><C4AC>ѡ<EFBFBD>е<EFBFBD>һ<EFBFBD><D2BB>
}
public void OpenMap()
{
if(dropMap.options.Count == 0)
{
UIWindow.Instance.ShowMessage("<22><><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD>ͼ");
return;
}
if(_curOpenMapId > 0)
{
UIWindow.Instance.ShowMessage("<22><><EFBFBD>ȹر<C8B9><D8B1><EFBFBD><EFBFBD>е<EFBFBD>ͼ");
return;
}
2025-06-16 21:26:36 +08:00
int mapId = Convert.ToInt32(dropMap.options[dropMap.value].text);
_curOpenMapId = mapId;
2025-06-19 13:32:33 +08:00
UIWindow.Instance.uiEditMapConfig.LoadMapConfig(mapId);
2025-06-16 23:59:28 +08:00
MapManager.Instance.LoadMapRegionSprites(_curOpenMapId);
2025-06-19 01:31:00 +08:00
MapManager.Instance.LoadMapObs(_curOpenMapId);
2025-06-19 13:32:33 +08:00
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);
2025-06-19 01:31:00 +08:00
//<2F><><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><C3A3><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (MapManager.Instance.map == null)
{
MapManager.Instance.CreateObs();
2025-06-19 13:32:33 +08:00
}
UICellInfo.Instance.ShowMapCellInfo();
2025-06-14 13:46:24 +08:00
}
public void CloseMap()
{
MapManager.Instance.CloseMap();
_curOpenMapId = -1;
}
public bool HasMap(string mapId)
{
2025-06-16 00:15:41 +08:00
return MapManager.Instance.allMaps.ContainsKey(mapId);
2025-06-14 13:46:24 +08:00
}
private void Update()
{
if (Input.GetKey(KeyCode.LeftControl) && Input.GetKey(KeyCode.S))
{
if (_saving)
return;
SaveAll();
}
}
public void SaveAll()
{
_saving = true;
Debug.Log("<22><><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>...");
2025-06-16 23:59:28 +08:00
MapManager.Instance.SaveRegionXML();
2025-06-19 13:32:33 +08:00
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("<22><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>");
2025-06-25 15:57:50 +08:00
_saving = false;
#if UNITY_EDITOR
AssetDatabase.Refresh();
#endif
2025-06-14 13:46:24 +08:00
}
}