vampire-like/Assets/GameMain/Scripts/Editor/UIRouterComponentEditor.cs

134 lines
5.0 KiB
C#

#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using SepCore.CustomComponent;
using SepCore.UI;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
namespace UIModule.Editor
{
[CustomEditor(typeof(UIRouterComponent))]
public class UIRouterComponentEditor : UnityEditor.Editor
{
private SerializedProperty _bindingsProperty;
private GUIContent[] _controllerDisplayNames;
private string[] _controllerTypeNames;
private ReorderableList _bindingsList;
private void OnEnable()
{
_bindingsProperty = serializedObject.FindProperty("_controllerBindings");
BuildControllerTypeOptions();
BuildReorderableList();
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.HelpBox("配置 UIFormType 到 IUIFormController 的映射。运行时 Awake 自动注册。",
MessageType.Info);
if (GUILayout.Button("Refresh Controller List"))
{
BuildControllerTypeOptions();
}
if (_bindingsList == null)
{
EditorGUILayout.HelpBox("Bindings list init failed.", MessageType.Error);
}
else
{
_bindingsList.DoLayoutList();
}
serializedObject.ApplyModifiedProperties();
}
private void BuildReorderableList()
{
if (_bindingsProperty == null)
{
return;
}
_bindingsList = new ReorderableList(serializedObject, _bindingsProperty, true, true, true, true);
_bindingsList.drawHeaderCallback = rect => { EditorGUI.LabelField(rect, "Controller Bindings"); };
_bindingsList.elementHeight = EditorGUIUtility.singleLineHeight * 2 + 8f;
_bindingsList.drawElementCallback = DrawElement;
_bindingsList.onAddCallback = list =>
{
_bindingsProperty.InsertArrayElementAtIndex(_bindingsProperty.arraySize);
SerializedProperty item = _bindingsProperty.GetArrayElementAtIndex(_bindingsProperty.arraySize - 1);
item.FindPropertyRelative("_controllerTypeName").stringValue = string.Empty;
item.FindPropertyRelative("_uiFormType").enumValueIndex = 0;
serializedObject.ApplyModifiedProperties();
};
}
private void BuildControllerTypeOptions()
{
List<Type> controllerTypes = new();
foreach (Type type in TypeCache.GetTypesDerivedFrom<IUIFormController>())
{
if (type.IsAbstract || type.IsInterface || type.ContainsGenericParameters)
{
continue;
}
controllerTypes.Add(type);
}
controllerTypes.Sort((a, b) => string.CompareOrdinal(a.FullName, b.FullName));
_controllerTypeNames = new string[controllerTypes.Count + 1];
_controllerDisplayNames = new GUIContent[controllerTypes.Count + 1];
_controllerTypeNames[0] = string.Empty;
_controllerDisplayNames[0] = new GUIContent("<None>");
for (int i = 0; i < controllerTypes.Count; i++)
{
Type type = controllerTypes[i];
_controllerTypeNames[i + 1] = type.AssemblyQualifiedName;
_controllerDisplayNames[i + 1] = new GUIContent(type.FullName);
}
}
private void DrawElement(Rect rect, int index, bool isActive, bool isFocused)
{
SerializedProperty item = _bindingsProperty.GetArrayElementAtIndex(index);
SerializedProperty formTypeProperty = item.FindPropertyRelative("_uiFormType");
SerializedProperty controllerTypeProperty = item.FindPropertyRelative("_controllerTypeName");
Rect row1 = new Rect(rect.x, rect.y + 2f, rect.width, EditorGUIUtility.singleLineHeight);
Rect row2 = new Rect(rect.x, rect.y + EditorGUIUtility.singleLineHeight + 6f, rect.width,
EditorGUIUtility.singleLineHeight);
EditorGUI.PropertyField(row1, formTypeProperty, new GUIContent("UI Form Type"));
DrawControllerPopup(row2, controllerTypeProperty);
}
private void DrawControllerPopup(Rect rect, SerializedProperty controllerTypeProperty)
{
int currentIndex = 0;
string currentValue = controllerTypeProperty.stringValue;
for (int i = 0; i < _controllerTypeNames.Length; i++)
{
if (_controllerTypeNames[i] == currentValue)
{
currentIndex = i;
break;
}
}
int selected =
EditorGUI.Popup(rect, new GUIContent("Controller Type"), currentIndex, _controllerDisplayNames);
controllerTypeProperty.stringValue = _controllerTypeNames[selected];
}
}
}
#endif