3DTD/Assets/Scripts/UI/UITooltips.cs

66 lines
2.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UITooltips : MonoBehaviour
{
// Not optimal but gamejam momento
[SerializeField] private GameObject tooltip;
[SerializeField] private EventTrigger ExtraExitCollider;
private EventTrigger uiEvents;
public int ButtonIndex = -1;
/// <summary>
/// First param is buy index
/// </summary>
public event Action<int> OnClick;
private void OnEnable()
{
uiEvents = GetComponent<EventTrigger>();
// Subscribe to PointerEnter event
EventTrigger.Entry pointerEnter = new EventTrigger.Entry();
pointerEnter.eventID = EventTriggerType.PointerEnter;
pointerEnter.callback.AddListener((data) => { OnEnter((PointerEventData)data); });
uiEvents.triggers.Add(pointerEnter);
// Subscribe to PointerEnter event
EventTrigger.Entry ExtraExitEnter = new EventTrigger.Entry();
ExtraExitEnter.eventID = EventTriggerType.PointerEnter;
ExtraExitEnter.callback.AddListener((data) => { OnExit((PointerEventData)data); });
ExtraExitCollider.triggers.Add(ExtraExitEnter);
// Subscribe to PointerExit event
EventTrigger.Entry pointerExit = new EventTrigger.Entry();
pointerExit.eventID = EventTriggerType.PointerExit;
pointerExit.callback.AddListener((data) => { OnExit((PointerEventData)data); });
uiEvents.triggers.Add(pointerExit);
// Subscribe to PointerClick event
EventTrigger.Entry pointerClick = new EventTrigger.Entry();
pointerClick.eventID = EventTriggerType.PointerClick;
pointerClick.callback.AddListener((data) => { m_OnClick((PointerEventData)data); });
uiEvents.triggers.Add(pointerClick);
}
private void OnEnter(PointerEventData eventData)
{
tooltip.SetActive(true);
}
private void OnExit(PointerEventData eventData)
{
tooltip.SetActive(false);
}
private void m_OnClick(PointerEventData data)
{
OnClick?.Invoke(ButtonIndex);
}
}