Files
Jeremy/Assets/Scripts/MyButton.cs
T

58 lines
1.2 KiB
C#
Raw Normal View History

2025-01-04 13:29:07 +01:00
using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class MyButton : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
{
[SerializeField]
private UnityEvent _onClick;
// references
2025-01-05 02:16:26 +01:00
[SerializeField]
2025-01-04 13:29:07 +01:00
private OutlineFx.OutlineFx _outlineFx;
2025-01-05 02:16:26 +01:00
[SerializeField]
public bool isActive = true;
2025-01-05 17:13:12 +01:00
private bool _isPointerOver = false;
public void SetActive(bool active)
{
isActive = active;
}
2025-01-05 02:16:26 +01:00
2025-01-04 18:19:10 +01:00
private void OnEnable()
2025-01-04 13:29:07 +01:00
{
2025-01-04 14:14:49 +01:00
if (!TryGetComponent(out Collider2D _))
{
Debug.LogError("MyButton needs a Collider2D to work", gameObject);
}
2025-01-05 02:16:26 +01:00
if (!_outlineFx)
{
_outlineFx = GetComponent<OutlineFx.OutlineFx>();
}
2025-01-04 14:14:49 +01:00
_outlineFx.enabled = false;
2025-01-04 13:29:07 +01:00
}
public void OnPointerClick(PointerEventData eventData)
{
2025-01-05 17:13:12 +01:00
if (isActive)
2025-01-05 02:16:26 +01:00
_onClick.Invoke();
2025-01-04 13:29:07 +01:00
}
public void OnPointerEnter(PointerEventData eventData)
{
2025-01-05 17:13:12 +01:00
_isPointerOver = true;
2025-01-04 13:29:07 +01:00
}
public void OnPointerExit(PointerEventData eventData)
{
2025-01-05 17:13:12 +01:00
_isPointerOver = false;
}
private void Update()
{
_outlineFx.enabled = _isPointerOver && isActive;
2025-01-04 13:29:07 +01:00
}
}