Files
Gen_Hack-and-Slash-Roguelite/Client/Assets/Scripts/UI/ItemUI.cs

91 lines
2.5 KiB
C#

using Base;
using System;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace UI
{
public class ItemUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler, ITick
{
[SerializeField] private Image textureUI;
[SerializeField] private TMP_Text countUI;
[SerializeField] private TMP_Text nameUI;
[SerializeField] private GameObject selectedOutline;
private Entity.InventorySlot _item;
private float timer = 0;
private float switchTime = 0;
private int texturePtr = 0;
public event Action OnPlayerSelect;
public bool Select
{
get => selectedOutline.activeSelf;
set => selectedOutline.SetActive(value);
}
public int SlotIndex { get; private set; } = -1;
public void Init(Entity.InventorySlot item, int index)
{
if (item == null)
{
switchTime = -1;
textureUI.gameObject.SetActive(false);
countUI.text = "";
nameUI.text = "";
return;
}
textureUI.gameObject.SetActive(true);
_item = item;
textureUI.sprite = item.Item.Icon[0];
countUI.text = item.Quantity.ToString();
nameUI.text = item.Item.Name;
nameUI.gameObject.SetActive(false);
Select = false;
SlotIndex = index;
if (item.Item.FPS > 0)
{
switchTime = 1f / item.Item.FPS;
}
else
{
switchTime = 0;
}
}
public void OnPointerEnter(PointerEventData eventData)
{
nameUI.gameObject.SetActive(true);
}
public void OnPointerExit(PointerEventData eventData)
{
nameUI.gameObject.SetActive(false);
}
public void OnPointerClick(PointerEventData eventData)
{
OnPlayerSelect?.Invoke();
}
public void Tick()
{
if (switchTime > 0)
{
timer += Time.deltaTime;
if (timer >= switchTime)
{
timer -= switchTime;
texturePtr++;
texturePtr %= _item.Item.Icon.Count;
textureUI.sprite = _item.Item.Icon[texturePtr];
}
}
}
}
}