Facebook
From Homer, 1 Week ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 86
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. using TMPro;
  7.  
  8. public class Slot : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
  9. {
  10.     public GameObject item;
  11.     public int ID;
  12.     public string type;
  13.     public string description;
  14.     public bool empty;
  15.  
  16.     public Transform slotIconGO;
  17.     public Sprite emptySlotSprite;
  18.     public Sprite icon;
  19.  
  20.     public int amount;
  21.     public TextMeshProUGUI amountText;
  22.  
  23.     public GameObject tooltipBackground;
  24.     public TextMeshProUGUI infoText;
  25.  
  26.     public void OnPointerClick(PointerEventData pointerEventData)
  27.     {
  28.         UseItem();
  29.     }
  30.  
  31.     public void OnPointerEnter(PointerEventData pointerEventData)
  32.     {
  33.         if (!empty)
  34.         {
  35.             tooltipBackground.SetActive(true);
  36.             infoText.text = type + "n" + description;
  37.         }
  38.     }
  39.  
  40.     public void OnPointerExit(PointerEventData pointerEventData)
  41.     {
  42.         tooltipBackground.SetActive(false);
  43.     }
  44.  
  45.     private void Start()
  46.     {
  47.         icon = GetComponent<Sprite>();
  48.        
  49.  
  50.         // Initialize slot with the manually added item
  51.         if (item != null)
  52.         {
  53.             Item itemComponent = item.GetComponent<Item>();
  54.             if (itemComponent != null)
  55.             {
  56.                 UpdateSlot(itemComponent.icon);
  57.                 amountText.text = amount.ToString();
  58.             }
  59.         }
  60.     }
  61.  
  62.     private void Update()
  63.     {
  64.         if (tooltipBackground.activeSelf)
  65.         {
  66.             tooltipBackground.transform.position = Input.mousePosition + new Vector3(10, -10, 0);
  67.         }
  68.     }
  69.  
  70.     public void UpdateSlot(Sprite newIcon)
  71.     {
  72.         icon = newIcon;
  73.         if (slotIconGO != null)
  74.         {
  75.             Image slotImage = slotIconGO.GetComponent<Image>();
  76.             if (slotImage != null)
  77.             {
  78.                 slotImage.sprite = icon;
  79.             }
  80.         }
  81.     }
  82.  
  83.     public void ClearSlot()
  84.     {
  85.         icon = null;
  86.         if (slotIconGO != null)
  87.         {
  88.             Image slotImage = slotIconGO.GetComponent<Image>();
  89.             if (slotImage != null)
  90.             {
  91.                 slotImage.sprite = emptySlotSprite;
  92.             }
  93.         }
  94.     }
  95.  
  96.     private void UseItem()
  97.     {
  98.         Item itemComponent = item.GetComponent<Item>();
  99.         if (itemComponent != null)
  100.         {
  101.             itemComponent.ItemUsage();
  102.         }
  103.        
  104.         if (!empty)
  105.         {
  106.             amount--;
  107.             if (amount <= 0)
  108.             {
  109.                 ClearSlot();
  110.             }
  111.             UpdateAmountText();
  112.         }
  113.     }
  114.  
  115.     void UpdateAmountText()
  116.     {
  117.         if (amountText != null)
  118.         {
  119.             if (amount > 1)
  120.             {
  121.                 amountText.text = amount.ToString();
  122.             }
  123.             else
  124.             {
  125.                 amountText.text = "";
  126.             }
  127.         }
  128.     }
  129.  
  130.     public void increaseAmount()
  131.     {
  132.         amount++;
  133.         amountText.text = amount.ToString();
  134.     }
  135. }
  136.