91 lines
2.3 KiB
C#
91 lines
2.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
namespace SepCore.UI
|
|
{
|
|
public class CommonButton : Button
|
|
{
|
|
[SerializeField] private Image _background = null;
|
|
|
|
private Color _originalBackgroundColor;
|
|
|
|
[SerializeField] private UnityEvent _onPointerEnterAction = new();
|
|
|
|
[SerializeField] private UnityEvent _onClickAction = new();
|
|
|
|
[SerializeField] private UnityEvent _onPointerExitAction = new();
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
_originalBackgroundColor = _background.color;
|
|
}
|
|
|
|
protected override void OnEnable()
|
|
{
|
|
_background.color = _originalBackgroundColor;
|
|
}
|
|
|
|
protected override void OnDisable()
|
|
{
|
|
_background.color = new Color(0.3f, 0.3f, 0.3f, 0.3f);
|
|
}
|
|
|
|
public override void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
if (eventData.button != PointerEventData.InputButton.Left)
|
|
{
|
|
return;
|
|
}
|
|
|
|
EnterHoverState();
|
|
}
|
|
|
|
public override void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
base.OnPointerDown(eventData);
|
|
|
|
_onClickAction?.Invoke();
|
|
}
|
|
|
|
public override void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
if (eventData.button != PointerEventData.InputButton.Left)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ExitHoverState();
|
|
}
|
|
|
|
public override void OnSelect(BaseEventData eventData)
|
|
{
|
|
_background.color = new Color(1f, 0.9f, 0.3f);
|
|
EnterHoverState();
|
|
}
|
|
|
|
public override void OnDeselect(BaseEventData eventData)
|
|
{
|
|
_background.color = _originalBackgroundColor;
|
|
ExitHoverState();
|
|
}
|
|
|
|
private void EnterHoverState()
|
|
{
|
|
if (EventSystem.current.currentSelectedGameObject != gameObject)
|
|
{
|
|
EventSystem.current.SetSelectedGameObject(this.gameObject);
|
|
}
|
|
|
|
_onPointerEnterAction.Invoke();
|
|
}
|
|
|
|
private void ExitHoverState()
|
|
{
|
|
_onPointerExitAction.Invoke();
|
|
}
|
|
}
|
|
}
|