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

72 lines
2.5 KiB
C#

using System;
using UnityEngine;
using UnityEngine.EventSystems;
public class UIManager : MonoBehaviour
{
[SerializeField] private Animator anim;
[SerializeField] private EventTrigger OpenerTrigger;
[SerializeField] private EventTrigger CloseTrigger;
private bool OpenerIsHovered = false;
private bool CloseIsHovered = false;
private bool CurrentShopState = false;
private void OnEnable()
{
// Subscribe to PointerEnter event
EventTrigger.Entry pointerOpenerEnter = new EventTrigger.Entry();
pointerOpenerEnter.eventID = EventTriggerType.PointerEnter;
pointerOpenerEnter.callback.AddListener((data) => { OnOpenerEnter((PointerEventData)data); });
OpenerTrigger.triggers.Add(pointerOpenerEnter);
// Subscribe to PointerExit event
EventTrigger.Entry pointerOpenerExit = new EventTrigger.Entry();
pointerOpenerExit.eventID = EventTriggerType.PointerExit;
pointerOpenerExit.callback.AddListener((data) => { OnOpenerExit((PointerEventData)data); });
OpenerTrigger.triggers.Add(pointerOpenerExit);
// Subscribe to PointerEnter event
EventTrigger.Entry pointerCloseTriggerEnter = new EventTrigger.Entry();
pointerCloseTriggerEnter.eventID = EventTriggerType.PointerEnter;
pointerCloseTriggerEnter.callback.AddListener((data) => { OnCloseTriggerEnter((PointerEventData)data); });
CloseTrigger.triggers.Add(pointerCloseTriggerEnter);
// Subscribe to PointerExit event
EventTrigger.Entry pointerCloseTriggerExit = new EventTrigger.Entry();
pointerCloseTriggerExit.eventID = EventTriggerType.PointerExit;
pointerCloseTriggerExit.callback.AddListener((data) => { OnCloseTriggerExit((PointerEventData)data); });
CloseTrigger.triggers.Add(pointerCloseTriggerExit);
}
private void OnCloseTriggerEnter(PointerEventData data)
{
CloseIsHovered = true;
UpdateShopState();
}
private void OnCloseTriggerExit(PointerEventData data)
{
CloseIsHovered = false;
UpdateShopState();
}
private void OnOpenerEnter(PointerEventData data)
{
OpenerIsHovered = true;
UpdateShopState();
}
private void OnOpenerExit(PointerEventData data)
{
OpenerIsHovered = false;
UpdateShopState();
}
private void UpdateShopState()
{
CurrentShopState = (CurrentShopState && !CloseIsHovered) || (OpenerIsHovered && !CloseIsHovered);
anim.SetBool("IsOpen", CurrentShopState);
}
}