You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Jeremy/Assets/Scripts/MyButton.cs

58 lines
1.2 KiB

using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class MyButton : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
{
[SerializeField]
private UnityEvent _onClick;
// references
[SerializeField]
private OutlineFx.OutlineFx _outlineFx;
[SerializeField]
public bool isActive = true;
private bool _isPointerOver = false;
public void SetActive(bool active)
{
isActive = active;
}
private void OnEnable()
{
if (!TryGetComponent(out Collider2D _))
{
Debug.LogError("MyButton needs a Collider2D to work", gameObject);
}
if (!_outlineFx)
{
_outlineFx = GetComponent<OutlineFx.OutlineFx>();
}
_outlineFx.enabled = false;
}
public void OnPointerClick(PointerEventData eventData)
{
if (isActive)
_onClick.Invoke();
}
public void OnPointerEnter(PointerEventData eventData)
{
_isPointerOver = true;
}
public void OnPointerExit(PointerEventData eventData)
{
_isPointerOver = false;
}
private void Update()
{
_outlineFx.enabled = _isPointerOver && isActive;
}
}