209 lines
7.8 KiB
C#
209 lines
7.8 KiB
C#
/*
|
|
* @Author: 白哉
|
|
* @Description: 编辑器
|
|
* @Date: 2025年04月22日 星期二 15:04:14
|
|
* @Modify:
|
|
*/
|
|
|
|
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using Logic.Achievement;
|
|
using Logic.CrashSight;
|
|
using Logic.HeroTask;
|
|
using Logic.Multilingual;
|
|
using RuntimeData;
|
|
using TH1_Logic.Core;
|
|
using TH1_Logic.HeroTask;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
|
|
namespace Logic.Editor
|
|
{
|
|
public class MapEditorWindow : EditorWindow
|
|
{
|
|
// 滑条
|
|
private Vector2 _barPosition;
|
|
|
|
// 背景
|
|
private GUIStyle _redBoxStyle;
|
|
private GUIStyle _whiteBoxStyle;
|
|
|
|
private List<string> _mapNameList;
|
|
private string _selectedMapName;
|
|
|
|
|
|
[MenuItem("Tools/地图编辑器")]
|
|
private static void ShowWindow()
|
|
{
|
|
var window = CreateWindow<MapEditorWindow>();
|
|
window.titleContent = new GUIContent("地图编辑器");
|
|
window.Show();
|
|
}
|
|
|
|
protected virtual void OnEnable()
|
|
{
|
|
EditorApplication.update += OnEditorUpdate;
|
|
RefreshMapNameList();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
EditorApplication.update -= OnEditorUpdate;
|
|
}
|
|
|
|
private void OnEditorUpdate()
|
|
{
|
|
// 每帧刷新窗口
|
|
Repaint();
|
|
}
|
|
|
|
private void OnGUI()
|
|
{
|
|
var grid = MapRecordManager.Instance.RecordGridData;
|
|
if (_redBoxStyle == null)
|
|
{
|
|
_redBoxStyle = InspectorUtils.GetHelpBoxStyle();
|
|
InspectorUtils.AddBorder(_redBoxStyle, new Color(0.5f, 0.4f, 0.4f, 0.6f));
|
|
}
|
|
if (_whiteBoxStyle == null)
|
|
{
|
|
_whiteBoxStyle = InspectorUtils.GetHelpBoxStyle();
|
|
InspectorUtils.AddBorder(_whiteBoxStyle, new Color(1f, 1f, 1f, 0.2f));
|
|
}
|
|
|
|
GUI.skin.button.wordWrap = true;
|
|
_barPosition = EditorGUILayout.BeginScrollView(_barPosition);
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
if (InspectorUtils.InspectorButtonWithTextWidth("刷新"))
|
|
{
|
|
RefreshMapNameList();
|
|
grid?.Renderer(Main.MapData)?.InstantUpdateGrid();
|
|
}
|
|
if (InspectorUtils.InspectorButtonWithTextWidth("保存当前地图"))
|
|
{
|
|
string defaultName = string.IsNullOrEmpty(_selectedMapName) ? "NewMap" : _selectedMapName;
|
|
// 使用 EditorApplication.delayCall 延迟到布局结束后再显示对话框
|
|
EditorApplication.delayCall += () =>
|
|
{
|
|
SaveMapDialog.Show($"保存确认", $"地图名称", defaultName, (string value) =>
|
|
{
|
|
if (!string.IsNullOrEmpty(value))
|
|
{
|
|
MapRecordManager.Instance.SaveMapRecord(Main.MapData, value);
|
|
_selectedMapName = value;
|
|
RefreshMapNameList();
|
|
}
|
|
});
|
|
};
|
|
}
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
// 切换存储地图
|
|
EditorGUILayout.BeginHorizontal();
|
|
if (_mapNameList == null || _mapNameList.Count == 0)
|
|
{
|
|
InspectorUtils.InspectorTextWidthRich($"<b> 暂无已存储地图 </b>");
|
|
}
|
|
else
|
|
{
|
|
InspectorUtils.InspectorTextWidthRich($"<b> 选择地图 : </b>");
|
|
var index = GetIndexInMapNameList(_selectedMapName);
|
|
index = EditorGUILayout.Popup(index, _mapNameList.ToArray(), GUILayout.Width(300));
|
|
_selectedMapName = _mapNameList[index];
|
|
if (InspectorUtils.InspectorButtonWithTextWidth("进入游戏"))
|
|
{
|
|
Main.Instance.MapConfig.IsCustomMap = true;
|
|
Main.Instance.MapConfig.MapName = _selectedMapName;
|
|
Main.Instance.StartMatch();
|
|
Main.Instance.MapConfig.IsCustomMap = false;
|
|
}
|
|
}
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
OnGUIGridData();
|
|
EditorGUILayout.EndScrollView();
|
|
}
|
|
|
|
private void OnGUIGridData()
|
|
{
|
|
var grid = MapRecordManager.Instance.RecordGridData;
|
|
if (grid == null) return;
|
|
EditorGUILayout.BeginVertical(_whiteBoxStyle);
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
InspectorUtils.InspectorTextWidthRich($"<b> ID : </b> {grid.Id} ");
|
|
InspectorUtils.InspectorTextWidthRich($"<b> 位置 : </b> x {grid.Pos.X} y {grid.Pos.Y} ");
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
InspectorUtils.InspectorTextWidthRich($"<b> CivId : </b> ");
|
|
grid.CivId = (uint)EditorGUILayout.IntField((int)grid.CivId, GUILayout.Width(150));
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
InspectorUtils.InspectorTextWidthRich($"<b> Terrain : </b> ");
|
|
grid.Terrain = (TerrainType) EditorGUILayout.EnumPopup(grid.Terrain, GUILayout.Width(150));
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
InspectorUtils.InspectorTextWidthRich($"<b> Feature : </b> ");
|
|
grid.Feature = (TerrainFeature) EditorGUILayout.EnumPopup(grid.Feature, GUILayout.Width(150));
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
InspectorUtils.InspectorTextWidthRich($"<b> Vegetation : </b> ");
|
|
grid.Vegetation = (Vegetation) EditorGUILayout.EnumPopup(grid.Vegetation, GUILayout.Width(150));
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
InspectorUtils.InspectorTextWidthRich($"<b> Resource : </b> ");
|
|
grid.Resource = (ResourceType) EditorGUILayout.EnumPopup(grid.Resource, GUILayout.Width(150));
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
InspectorUtils.InspectorTextWidthRich($"<b> Wonder : </b> ");
|
|
grid.Wonder = (WonderLibrary) EditorGUILayout.EnumPopup(grid.Wonder, GUILayout.Width(150));
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
InspectorUtils.InspectorTextWidthRich($"<b> ResourceUnderBuilding : </b> ");
|
|
grid.ResourceUnderBuilding = (ResourceType) EditorGUILayout.EnumPopup(grid.ResourceUnderBuilding, GUILayout.Width(150));
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
EditorGUILayout.EndVertical();
|
|
}
|
|
|
|
private void RefreshMapNameList()
|
|
{
|
|
_mapNameList ??= new List<string>();
|
|
_mapNameList.Clear();
|
|
string folderPath = "Assets/BundleResources/MapRecord";
|
|
if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath);
|
|
|
|
string[] files = Directory.GetFiles(folderPath, "*.bytes");
|
|
foreach (string file in files)
|
|
{
|
|
string fileName = Path.GetFileNameWithoutExtension(file);
|
|
if (!string.IsNullOrEmpty(fileName))
|
|
{
|
|
_mapNameList.Add(fileName);
|
|
}
|
|
}
|
|
|
|
LogSystem.LogInfo($"Init: 扫描完成,找到 {_mapNameList.Count} 个地图记录");
|
|
}
|
|
|
|
private int GetIndexInMapNameList(string mapName)
|
|
{
|
|
if (_mapNameList == null) return 0;
|
|
for (int i = 0; i < _mapNameList.Count; i++)
|
|
{
|
|
if (_mapNameList[i] == mapName) return i;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
} |