com.alicizax.unity.ui.exten.../Editor/UX/Hotkey/HotkeyComponentEditor.cs

79 lines
3.0 KiB
C#
Raw Normal View History

2026-03-20 13:11:41 +08:00
using AlicizaX.UI.Runtime;
2026-03-19 19:50:32 +08:00
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace UnityEditor.UI
{
[CustomEditor(typeof(HotkeyComponent), true)]
public class HotkeyComponentEditor : UnityEditor.Editor
{
private SerializedProperty _hotkeyAction;
private SerializedProperty _hotkeyPressType;
private SerializedProperty _component;
private SerializedProperty _holder;
2026-03-19 19:50:32 +08:00
private void OnEnable()
{
_component = serializedObject.FindProperty("_component");
_holder = serializedObject.FindProperty("_holder");
2026-03-19 19:50:32 +08:00
_hotkeyAction = serializedObject.FindProperty("_hotkeyAction");
_hotkeyPressType = serializedObject.FindProperty("_hotkeyPressType");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
2026-03-20 13:11:41 +08:00
HotkeyComponent hotkeyComponent = (HotkeyComponent)target;
EditorGUILayout.HelpBox(
"Hotkeys auto-register to the nearest UIHolderObjectBase at runtime.",
MessageType.Info
);
if (_holder.objectReferenceValue == null)
2026-03-20 13:11:41 +08:00
{
EditorGUILayout.HelpBox(
"No UIHolderObjectBase was found in parents. This hotkey will not register at runtime.",
MessageType.Warning
);
}
2026-03-19 19:50:32 +08:00
if (_component.objectReferenceValue == null)
{
2026-03-20 13:11:41 +08:00
EditorGUILayout.HelpBox("No submit target was found on this object.", MessageType.Error);
2026-03-19 19:50:32 +08:00
if (hotkeyComponent.TryGetComponent(typeof(ISubmitHandler), out Component submitHandler))
{
_component.objectReferenceValue = submitHandler;
}
}
else if (_component.objectReferenceValue is not ISubmitHandler)
{
EditorGUILayout.HelpBox("Submit target must implement ISubmitHandler. The invalid reference will be cleared.", MessageType.Error);
_component.objectReferenceValue = null;
}
if (_hotkeyAction.objectReferenceValue == null)
{
EditorGUILayout.HelpBox("Input Action is required. This hotkey will not register at runtime.", MessageType.Error);
}
2026-03-19 19:50:32 +08:00
using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
{
EditorGUILayout.LabelField("Hotkey Setting", EditorStyles.boldLabel);
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.PropertyField(_component, new GUIContent("Component"));
EditorGUILayout.PropertyField(_holder, new GUIContent("Holder"));
2026-03-19 19:50:32 +08:00
EditorGUI.EndDisabledGroup();
2026-03-20 13:11:41 +08:00
EditorGUILayout.PropertyField(_hotkeyAction, new GUIContent("Input Action"));
EditorGUILayout.PropertyField(_hotkeyPressType, new GUIContent("Press Type"));
2026-03-19 19:50:32 +08:00
}
serializedObject.ApplyModifiedProperties();
}
}
}