using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using TMPro; public class Slot : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler { public GameObject item; public int ID; public string type; public string description; public bool empty; public Transform slotIconGO; public Sprite emptySlotSprite; public Sprite icon; public int amount; public TextMeshProUGUI amountText; public GameObject tooltipBackground; public TextMeshProUGUI infoText; public void OnPointerClick(PointerEventData pointerEventData) { UseItem(); } public void OnPointerEnter(PointerEventData pointerEventData) { if (!empty) { tooltipBackground.SetActive(true); infoText.text = type + "n" + description; } } public void OnPointerExit(PointerEventData pointerEventData) { tooltipBackground.SetActive(false); } private void Start() { icon = GetComponent<Sprite>(); // Initialize slot with the manually added item if (item != null) { Item itemComponent = item.GetComponent<Item>(); if (itemComponent != null) { UpdateSlot(itemComponent.icon); amountText.text = amount.ToString(); } } } private void Update() { if (tooltipBackground.activeSelf) { tooltipBackground.transform.position = Input.mousePosition + new Vector3(10, -10, 0); } } public void UpdateSlot(Sprite newIcon) { icon = newIcon; if (slotIconGO != null) { Image slotImage = slotIconGO.GetComponent<Image>(); if (slotImage != null) { slotImage.sprite = icon; } } } public void ClearSlot() { icon = null; if (slotIconGO != null) { Image slotImage = slotIconGO.GetComponent<Image>(); if (slotImage != null) { slotImage.sprite = emptySlotSprite; } } } private void UseItem() { Item itemComponent = item.GetComponent<Item>(); if (itemComponent != null) { itemComponent.ItemUsage(); } if (!empty) { amount--; if (amount <= 0) { ClearSlot(); } UpdateAmountText(); } } void UpdateAmountText() { if (amountText != null) { if (amount > 1) { amountText.text = amount.ToString(); } else { amountText.text = ""; } } } public void increaseAmount() { amount++; amountText.text = amount.ToString(); } }