84 lines
2.3 KiB
C#
84 lines
2.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.Serialization;
|
|
|
|
namespace GeometryTD.UI
|
|
{
|
|
public class CommonButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler,
|
|
IPointerUpHandler
|
|
{
|
|
private const float FadeTime = 0.3f;
|
|
private const float OnHoverAlpha = 0.7f;
|
|
private const float OnClickAlpha = 0.6f;
|
|
|
|
[FormerlySerializedAs("m_OnHover")] [SerializeField]
|
|
private UnityEvent _onHover = null;
|
|
|
|
[FormerlySerializedAs("m_OnClick")] [SerializeField]
|
|
private UnityEvent _onClick = null;
|
|
|
|
[SerializeField] private UnityEvent _onHoverEnd = null;
|
|
|
|
[SerializeField] private bool _allowFade = true;
|
|
|
|
private CanvasGroup _canvasGroup = null;
|
|
|
|
private void Awake()
|
|
{
|
|
_canvasGroup = gameObject.GetOrAddComponent<CanvasGroup>();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
_canvasGroup.alpha = 1f;
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
if (eventData.button != PointerEventData.InputButton.Left)
|
|
{
|
|
return;
|
|
}
|
|
|
|
StopAllCoroutines();
|
|
if (_allowFade)
|
|
StartCoroutine(_canvasGroup.FadeToAlpha(OnHoverAlpha, FadeTime));
|
|
_onHover.Invoke();
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
if (eventData.button != PointerEventData.InputButton.Left)
|
|
{
|
|
return;
|
|
}
|
|
|
|
StopAllCoroutines();
|
|
if (_allowFade)
|
|
StartCoroutine(_canvasGroup.FadeToAlpha(1f, FadeTime));
|
|
_onHoverEnd?.Invoke();
|
|
}
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
if (eventData.button != PointerEventData.InputButton.Left)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_canvasGroup.alpha = OnClickAlpha;
|
|
_onClick.Invoke();
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
if (eventData.button != PointerEventData.InputButton.Left)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_canvasGroup.alpha = OnHoverAlpha;
|
|
}
|
|
}
|
|
} |