89 lines
2.2 KiB
C#
89 lines
2.2 KiB
C#
#if INPUTSYSTEM_SUPPORT
|
|
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 = EHotkeyPressType.Performed;
|
|
|
|
public InputActionReference HotkeyAction
|
|
{
|
|
get => _hotkeyAction;
|
|
set => _hotkeyAction = value;
|
|
}
|
|
|
|
EHotkeyPressType IHotkeyTrigger.HotkeyPressType
|
|
{
|
|
get => _hotkeyPressType;
|
|
set => _hotkeyPressType = value;
|
|
}
|
|
|
|
private void Reset()
|
|
{
|
|
AutoAssignTarget();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
AutoAssignTarget();
|
|
((IHotkeyTrigger)this).BindHotKey();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
((IHotkeyTrigger)this).UnBindHotKey();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
((IHotkeyTrigger)this).UnBindHotKey();
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
private void OnValidate()
|
|
{
|
|
AutoAssignTarget();
|
|
}
|
|
#endif
|
|
|
|
void IHotkeyTrigger.HotkeyActionTrigger()
|
|
{
|
|
if (!isActiveAndEnabled || _component == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (_component is ISubmitHandler)
|
|
{
|
|
ExecuteEvents.Execute(
|
|
_component.gameObject,
|
|
new BaseEventData(EventSystem.current),
|
|
ExecuteEvents.submitHandler
|
|
);
|
|
return;
|
|
}
|
|
|
|
Debug.LogWarning($"{nameof(HotkeyComponent)} target must implement {nameof(ISubmitHandler)}: {_component.name}", this);
|
|
}
|
|
|
|
private void AutoAssignTarget()
|
|
{
|
|
if (_component != null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (TryGetComponent(typeof(ISubmitHandler), out Component submitHandler))
|
|
{
|
|
_component = submitHandler;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|