com.alicizax.unity.ui.exten.../Runtime/UXComponent/Hotkey/HotkeyComponent.cs

142 lines
3.4 KiB
C#
Raw Normal View History

2026-03-20 13:11:41 +08:00
#if INPUTSYSTEM_SUPPORT
using AlicizaX.UI.Runtime;
2026-03-19 19:50:32 +08:00
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
namespace UnityEngine.UI
{
[DisallowMultipleComponent]
public sealed class HotkeyComponent : MonoBehaviour, IHotkeyTrigger
{
[SerializeField] private Component _component;
[SerializeField] private UIHolderObjectBase _holder;
2026-03-19 19:50:32 +08:00
[SerializeField] private InputActionReference _hotkeyAction;
2026-03-20 13:11:41 +08:00
[SerializeField] private EHotkeyPressType _hotkeyPressType = EHotkeyPressType.Performed;
2026-03-19 19:50:32 +08:00
private ISubmitHandler _submitHandler;
private BaseEventData _eventData;
private EventSystem _eventSystem;
2026-03-19 19:50:32 +08:00
public InputActionReference HotkeyAction
{
get => _hotkeyAction;
set => _hotkeyAction = value;
}
public EHotkeyPressType HotkeyPressType => _hotkeyPressType;
public UIHolderObjectBase HotkeyHolder => _holder;
private void Reset()
2026-03-19 19:50:32 +08:00
{
AutoAssignTarget();
AutoAssignHolder();
2026-03-19 19:50:32 +08:00
}
private void Awake()
2026-03-20 13:11:41 +08:00
{
AutoAssignTarget();
AutoAssignHolder();
CacheTarget();
CacheEventData();
2026-03-20 13:11:41 +08:00
}
private void OnEnable()
{
AutoAssignTarget();
AutoAssignHolder();
CacheTarget();
2026-03-20 13:11:41 +08:00
((IHotkeyTrigger)this).BindHotKey();
}
private void OnDisable()
{
((IHotkeyTrigger)this).UnBindHotKey();
}
private void OnApplicationFocus(bool hasFocus)
{
if (hasFocus)
{
CacheEventData();
}
}
2026-03-20 13:11:41 +08:00
private void OnDestroy()
{
((IHotkeyTrigger)this).UnBindHotKey();
_submitHandler = null;
_eventData = null;
_eventSystem = null;
2026-03-20 13:11:41 +08:00
}
#if UNITY_EDITOR
private void OnValidate()
{
AutoAssignTarget();
AutoAssignHolder();
CacheTarget();
if (_component != null && _submitHandler == null)
{
_component = null;
}
2026-03-20 13:11:41 +08:00
}
#endif
public void HotkeyActionTrigger()
2026-03-19 19:50:32 +08:00
{
if (!isActiveAndEnabled || _submitHandler == null)
{
return;
}
if (_eventData == null)
2026-03-20 13:11:41 +08:00
{
return;
}
if (!ReferenceEquals(_eventSystem, EventSystem.current))
2026-03-20 13:11:41 +08:00
{
return;
}
_submitHandler.OnSubmit(_eventData);
2026-03-20 13:11:41 +08:00
}
private void AutoAssignTarget()
{
if (_component != null)
{
return;
}
if (TryGetComponent(typeof(ISubmitHandler), out Component submitHandler))
2026-03-19 20:16:58 +08:00
{
2026-03-20 13:11:41 +08:00
_component = submitHandler;
2026-03-19 20:16:58 +08:00
}
2026-03-19 19:50:32 +08:00
}
private void AutoAssignHolder()
{
if (_holder != null && _holder.IsValid())
{
return;
}
_holder = GetComponentInParent<UIHolderObjectBase>(true);
}
private void CacheTarget()
{
_submitHandler = _component as ISubmitHandler;
}
private void CacheEventData()
{
_eventSystem = EventSystem.current;
_eventData = new BaseEventData(_eventSystem);
}
2026-03-19 19:50:32 +08:00
}
}
2026-03-20 13:11:41 +08:00
#endif