mirror of
https://github.com/DerTyp7/harvestdale-unity.git
synced 2025-10-29 04:42:08 +01:00
62 lines
1.6 KiB
C#
62 lines
1.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.EventSystems;
|
|
public class SlotUI : MonoBehaviour, IDropHandler
|
|
{
|
|
public int slotIndex;
|
|
[SerializeField] private GameObject contentObject;
|
|
[SerializeField] private InventoryItem inventoryItem;
|
|
[SerializeField] private Image itemSpriteImage;
|
|
[SerializeField] private TMPro.TextMeshProUGUI quantityText;
|
|
|
|
private Inventory playerInventory;
|
|
private void Start()
|
|
{
|
|
UpdateSlot();
|
|
contentObject.GetComponent<DraggableSlotContentUI>().slotIndex = slotIndex;
|
|
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
|
|
}
|
|
public void SetInventoryItem(InventoryItem newInventoryItem)
|
|
{
|
|
inventoryItem = newInventoryItem;
|
|
UpdateSlot();
|
|
}
|
|
|
|
private void UpdateSlot()
|
|
{
|
|
if (inventoryItem != null)
|
|
{
|
|
itemSpriteImage.sprite = inventoryItem?.item?.sprite ?? null;
|
|
if (itemSpriteImage.sprite == null)
|
|
{
|
|
itemSpriteImage.color = new Color(0, 0, 0, 0);
|
|
}
|
|
else
|
|
{
|
|
itemSpriteImage.color = new Color(255, 255, 255, 1);
|
|
}
|
|
|
|
quantityText.SetText(inventoryItem.count != 0 ? inventoryItem.count.ToString() : "");
|
|
}
|
|
else
|
|
{
|
|
itemSpriteImage.sprite = null;
|
|
quantityText.SetText("");
|
|
itemSpriteImage.color = new Color(0, 0, 0, 0);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnDrop(PointerEventData eventData)
|
|
{
|
|
Debug.Log("DROP");
|
|
DraggableSlotContentUI dropped = eventData.pointerDrag.GetComponent<DraggableSlotContentUI>();
|
|
playerInventory.SwapItems(dropped.slotIndex, slotIndex);
|
|
}
|
|
|
|
}
|