From bc554a506237ca7340b02542ccbfbb45eddb7089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Thu, 19 Mar 2026 19:50:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E7=A6=BBHotkey=E8=84=B1=E7=A6=BB?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/UX/Button/UButtonEditor.cs | 21 -------- Editor/UX/Hotkey/HotkeyComponentEditor.cs | 49 +++++++++++++++++++ .../UX/Hotkey/HotkeyComponentEditor.cs.meta | 3 ++ Editor/UX/Toggle/UXToggleEditor.cs | 19 ------- Runtime/UXComponent/Button/UXButton.cs | 37 -------------- Runtime/UXComponent/Group/UXToggle.cs | 40 --------------- Runtime/UXComponent/Hotkey/HotkeyComponent.cs | 34 +++++++++++++ .../Hotkey/HotkeyComponent.cs.meta | 3 ++ Runtime/UXComponent/Hotkey/IHotkeyTrigger.cs | 2 +- 9 files changed, 90 insertions(+), 118 deletions(-) create mode 100644 Editor/UX/Hotkey/HotkeyComponentEditor.cs create mode 100644 Editor/UX/Hotkey/HotkeyComponentEditor.cs.meta create mode 100644 Runtime/UXComponent/Hotkey/HotkeyComponent.cs create mode 100644 Runtime/UXComponent/Hotkey/HotkeyComponent.cs.meta diff --git a/Editor/UX/Button/UButtonEditor.cs b/Editor/UX/Button/UButtonEditor.cs index bd4d681..3a471be 100644 --- a/Editor/UX/Button/UButtonEditor.cs +++ b/Editor/UX/Button/UButtonEditor.cs @@ -20,12 +20,6 @@ namespace UnityEditor.UI { SerializedProperty m_OnClickProperty; - -#if INPUTSYSTEM_SUPPORT - private SerializedProperty _hotKeyRefrence; - private SerializedProperty _hotkeyPressType; -#endif - private SerializedProperty hoverAudioClip; private SerializedProperty clickAudioClip; @@ -35,12 +29,6 @@ namespace UnityEditor.UI m_OnClickProperty = serializedObject.FindProperty("m_OnClick"); - -#if INPUTSYSTEM_SUPPORT - _hotKeyRefrence = serializedObject.FindProperty("_hotkeyAction"); - _hotkeyPressType = serializedObject.FindProperty("_hotkeyPressType"); -#endif - hoverAudioClip = serializedObject.FindProperty("hoverAudioClip"); clickAudioClip = serializedObject.FindProperty("clickAudioClip"); @@ -64,15 +52,6 @@ namespace UnityEditor.UI serializedObject.Update(); EditorGUILayout.PropertyField(m_OnClickProperty); -#if INPUTSYSTEM_SUPPORT - using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox)) - { - EditorGUILayout.LabelField("Hotkey Setting", EditorStyles.boldLabel); - EditorGUILayout.PropertyField(_hotKeyRefrence, new GUIContent("InputAction")); - EditorGUILayout.PropertyField(_hotkeyPressType, new GUIContent("PressType")); - } - -#endif serializedObject.ApplyModifiedProperties(); } diff --git a/Editor/UX/Hotkey/HotkeyComponentEditor.cs b/Editor/UX/Hotkey/HotkeyComponentEditor.cs new file mode 100644 index 0000000..5adcf6f --- /dev/null +++ b/Editor/UX/Hotkey/HotkeyComponentEditor.cs @@ -0,0 +1,49 @@ +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 void OnEnable() + { + _component = serializedObject.FindProperty("_component"); + _hotkeyAction = serializedObject.FindProperty("_hotkeyAction"); + _hotkeyPressType = serializedObject.FindProperty("_hotkeyPressType"); + } + + public override void OnInspectorGUI() + { + serializedObject.Update(); + if (_component.objectReferenceValue == null) + { + EditorGUILayout.HelpBox("物体身上不存在ISubmitHandler得到组件", MessageType.Error); + HotkeyComponent hotkeyComponent = (HotkeyComponent)target; + if (hotkeyComponent.TryGetComponent(typeof(ISubmitHandler), out Component submitHandler)) + { + _component.objectReferenceValue = submitHandler; + } + } + + using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox)) + { + EditorGUILayout.LabelField("Hotkey Setting", EditorStyles.boldLabel); + + EditorGUI.BeginDisabledGroup(true); + EditorGUILayout.PropertyField(_component, new GUIContent("Component")); + EditorGUI.EndDisabledGroup(); + + EditorGUILayout.PropertyField(_hotkeyAction, new GUIContent("输入映射")); + EditorGUILayout.PropertyField(_hotkeyPressType, new GUIContent("触发类型")); + } + + serializedObject.ApplyModifiedProperties(); + } + } +} diff --git a/Editor/UX/Hotkey/HotkeyComponentEditor.cs.meta b/Editor/UX/Hotkey/HotkeyComponentEditor.cs.meta new file mode 100644 index 0000000..b3b883e --- /dev/null +++ b/Editor/UX/Hotkey/HotkeyComponentEditor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 939657bb1b5f4ad5b0836fa8fae2bef3 +timeCreated: 1773919464 \ No newline at end of file diff --git a/Editor/UX/Toggle/UXToggleEditor.cs b/Editor/UX/Toggle/UXToggleEditor.cs index 235b694..366de99 100644 --- a/Editor/UX/Toggle/UXToggleEditor.cs +++ b/Editor/UX/Toggle/UXToggleEditor.cs @@ -17,11 +17,6 @@ namespace UnityEditor.UI SerializedProperty m_GroupProperty; SerializedProperty m_IsOnProperty; -#if INPUTSYSTEM_SUPPORT - private SerializedProperty _hotKeyRefrence; - private SerializedProperty _hotkeyPressType; -#endif - private SerializedProperty hoverAudioClip; private SerializedProperty clickAudioClip; @@ -35,11 +30,6 @@ namespace UnityEditor.UI m_IsOnProperty = serializedObject.FindProperty("m_IsOn"); m_OnValueChangedProperty = serializedObject.FindProperty("onValueChanged"); -#if INPUTSYSTEM_SUPPORT - _hotKeyRefrence = serializedObject.FindProperty("_hotkeyAction"); - _hotkeyPressType = serializedObject.FindProperty("_hotkeyPressType"); -#endif - hoverAudioClip = serializedObject.FindProperty("hoverAudioClip"); clickAudioClip = serializedObject.FindProperty("clickAudioClip"); @@ -62,15 +52,6 @@ namespace UnityEditor.UI serializedObject.Update(); EditorGUILayout.PropertyField(m_OnValueChangedProperty); -#if INPUTSYSTEM_SUPPORT - using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox)) - { - EditorGUILayout.LabelField("Hotkey Setting", EditorStyles.boldLabel); - EditorGUILayout.PropertyField(_hotKeyRefrence, new GUIContent("InputAction")); - EditorGUILayout.PropertyField(_hotkeyPressType, new GUIContent("PressType")); - } - -#endif serializedObject.ApplyModifiedProperties(); } diff --git a/Runtime/UXComponent/Button/UXButton.cs b/Runtime/UXComponent/Button/UXButton.cs index 59760aa..6e1f12e 100644 --- a/Runtime/UXComponent/Button/UXButton.cs +++ b/Runtime/UXComponent/Button/UXButton.cs @@ -5,48 +5,11 @@ using AlicizaX.UI; using AlicizaX.UI.Extension; using UnityEngine.EventSystems; -#if INPUTSYSTEM_SUPPORT -using UnityEngine.InputSystem; -#endif - namespace UnityEngine.UI { [AddComponentMenu("UI/UXButton", 30)] public class UXButton : UXSelectable, IPointerClickHandler, ISubmitHandler, IButton -#if INPUTSYSTEM_SUPPORT - , IHotkeyTrigger -#endif { -#if INPUTSYSTEM_SUPPORT - - InputActionReference IHotkeyTrigger.HotkeyAction - { - get => _hotkeyAction; - set => _hotkeyAction = value; - } - - EHotkeyPressType IHotkeyTrigger.HotkeyPressType - { - get => _hotkeyPressType; - set => _hotkeyPressType = value; - } - - void IHotkeyTrigger.HotkeyActionTrigger() - { - if (interactable) - { - OnSubmit(null); - } - } - - [SerializeField] internal InputActionReference _hotkeyAction; - [SerializeField] internal EHotkeyPressType _hotkeyPressType; - - public InputActionReference HotKeyRefrence - { - get { return _hotkeyAction; } - } -#endif [SerializeField] private AudioClip hoverAudioClip; [SerializeField] private AudioClip clickAudioClip; diff --git a/Runtime/UXComponent/Group/UXToggle.cs b/Runtime/UXComponent/Group/UXToggle.cs index d163f0e..15c42c2 100644 --- a/Runtime/UXComponent/Group/UXToggle.cs +++ b/Runtime/UXComponent/Group/UXToggle.cs @@ -1,22 +1,12 @@ using System; -using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; -#if INPUTSYSTEM_SUPPORT -using UnityEngine.InputSystem; -#endif -#if UNITY_EDITOR -using UnityEditor; -#endif namespace UnityEngine.UI { [AddComponentMenu("UI/UXToggle", 30)] [RequireComponent(typeof(RectTransform))] public class UXToggle : UXSelectable, IPointerClickHandler, ISubmitHandler, ICanvasElement -#if INPUTSYSTEM_SUPPORT - , IHotkeyTrigger -#endif { [Serializable] public class ToggleEvent : UnityEvent @@ -249,36 +239,6 @@ namespace UnityEngine.UI UXComponentExtensionsHelper.AudioHelper.PlayAudio(clip); } -#if INPUTSYSTEM_SUPPORT - - InputActionReference IHotkeyTrigger.HotkeyAction - { - get => _hotkeyAction; - set => _hotkeyAction = value; - } - - EHotkeyPressType IHotkeyTrigger.HotkeyPressType - { - get => _hotkeyPressType; - set => _hotkeyPressType = value; - } - - void IHotkeyTrigger.HotkeyActionTrigger() - { - if (interactable) - { - OnSubmit(null); - } - } - - [SerializeField] internal InputActionReference _hotkeyAction; - [SerializeField] internal EHotkeyPressType _hotkeyPressType; - - public InputActionReference HotKeyRefrence - { - get { return _hotkeyAction; } - } -#endif [SerializeField] private AudioClip hoverAudioClip; [SerializeField] private AudioClip clickAudioClip; diff --git a/Runtime/UXComponent/Hotkey/HotkeyComponent.cs b/Runtime/UXComponent/Hotkey/HotkeyComponent.cs new file mode 100644 index 0000000..27d1f26 --- /dev/null +++ b/Runtime/UXComponent/Hotkey/HotkeyComponent.cs @@ -0,0 +1,34 @@ +using System; +using UnityEngine.EventSystems; +using UnityEngine.InputSystem; + +namespace UnityEngine.UI +{ + [DisallowMultipleComponent] + public sealed class HotkeyComponent : MonoBehaviour, IHotkeyTrigger + { + [SerializeField] private Component _component; + + [SerializeField] private InputActionReference _hotkeyAction; + + [SerializeField] private EHotkeyPressType _hotkeyPressType; + + + public InputActionReference HotkeyAction + { + get => _hotkeyAction; + set => _hotkeyAction = value; + } + + EHotkeyPressType IHotkeyTrigger.HotkeyPressType + { + get => _hotkeyPressType; + set => _hotkeyPressType = value; + } + + void IHotkeyTrigger.HotkeyActionTrigger() + { + + } + } +} diff --git a/Runtime/UXComponent/Hotkey/HotkeyComponent.cs.meta b/Runtime/UXComponent/Hotkey/HotkeyComponent.cs.meta new file mode 100644 index 0000000..3c7db8b --- /dev/null +++ b/Runtime/UXComponent/Hotkey/HotkeyComponent.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ad0e473486c04dc19b468f2b20675091 +timeCreated: 1773912951 \ No newline at end of file diff --git a/Runtime/UXComponent/Hotkey/IHotkeyTrigger.cs b/Runtime/UXComponent/Hotkey/IHotkeyTrigger.cs index 224831f..3241970 100644 --- a/Runtime/UXComponent/Hotkey/IHotkeyTrigger.cs +++ b/Runtime/UXComponent/Hotkey/IHotkeyTrigger.cs @@ -5,7 +5,7 @@ namespace UnityEngine.UI { public interface IHotkeyTrigger { - internal InputActionReference HotkeyAction { get; set; } + public InputActionReference HotkeyAction { get; } internal EHotkeyPressType HotkeyPressType { get; set; } internal void HotkeyActionTrigger(); }