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

72 lines
2.5 KiB
C#
Raw Normal View History

2024-04-21 01:43:22 +02:00
using System;
2024-04-20 17:52:56 +02:00
using UnityEngine;
2024-04-21 01:43:22 +02:00
using UnityEngine.EventSystems;
2024-04-20 17:52:56 +02:00
public class UIManager : MonoBehaviour
{
2024-04-21 01:43:22 +02:00
[SerializeField] private Animator anim;
[SerializeField] private EventTrigger OpenerTrigger;
[SerializeField] private EventTrigger CloseTrigger;
2024-04-20 17:52:56 +02:00
2024-04-21 01:43:22 +02:00
private bool OpenerIsHovered = false;
private bool CloseIsHovered = false;
2024-04-20 17:52:56 +02:00
2024-04-21 01:43:22 +02:00
private bool CurrentShopState = false;
2024-04-20 17:52:56 +02:00
2024-04-21 01:43:22 +02:00
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);
}
2024-04-20 17:52:56 +02:00
2024-04-21 01:43:22 +02:00
private void OnCloseTriggerEnter(PointerEventData data)
{
CloseIsHovered = true;
UpdateShopState();
}
2024-04-20 17:52:56 +02:00
2024-04-21 01:43:22 +02:00
private void OnCloseTriggerExit(PointerEventData data)
2024-04-20 17:52:56 +02:00
{
2024-04-21 01:43:22 +02:00
CloseIsHovered = false;
UpdateShopState();
2024-04-20 17:52:56 +02:00
}
2024-04-21 01:43:22 +02:00
private void OnOpenerEnter(PointerEventData data)
2024-04-20 17:52:56 +02:00
{
2024-04-21 01:43:22 +02:00
OpenerIsHovered = true;
UpdateShopState();
2024-04-20 17:52:56 +02:00
}
2024-04-21 01:43:22 +02:00
private void OnOpenerExit(PointerEventData data)
2024-04-20 17:52:56 +02:00
{
2024-04-21 01:43:22 +02:00
OpenerIsHovered = false;
UpdateShopState();
2024-04-20 17:52:56 +02:00
}
2024-04-21 01:43:22 +02:00
private void UpdateShopState()
{
CurrentShopState = (CurrentShopState && !CloseIsHovered) || (OpenerIsHovered && !CloseIsHovered);
anim.SetBool("IsOpen", CurrentShopState);
}
2024-04-20 17:52:56 +02:00
}