69 lines
1.8 KiB
C#
69 lines
1.8 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class InventorySlot : MonoBehaviour, IDropHandler
|
|
{
|
|
// public Image icon;
|
|
// public Text itemName;
|
|
public GameItem gameItem;
|
|
|
|
public GameObject inventoryItemPrefab;
|
|
// private Transform inventoryItemContainer;
|
|
|
|
public void OnDrop(PointerEventData eventData)
|
|
{
|
|
if (eventData.pointerDrag != null)
|
|
eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition =
|
|
GetComponent<RectTransform>().anchoredPosition;
|
|
}
|
|
|
|
|
|
public void SetInventoryItemPrefab(GameObject prefab)
|
|
{
|
|
inventoryItemPrefab = prefab;
|
|
}
|
|
|
|
|
|
public void AssignGameItem(GameItem newItem)
|
|
{
|
|
gameItem = newItem;
|
|
|
|
|
|
if (inventoryItemPrefab == null)
|
|
{
|
|
Debug.Log("InventoryItemPrefab not set");
|
|
return;
|
|
}
|
|
|
|
|
|
var entry = Instantiate(inventoryItemPrefab, transform);
|
|
|
|
var itemName = entry.transform.Find("ItemName").GetComponent<TMP_Text>();
|
|
var qty = entry.transform.Find("qty").GetComponent<TMP_Text>();
|
|
var icon = entry.transform.Find("Icon").GetComponent<Image>();
|
|
|
|
|
|
itemName.text = newItem.itemName;
|
|
qty.text = newItem.qty;
|
|
icon.sprite = newItem.icon;
|
|
}
|
|
|
|
public void GenerateInventoryItem()
|
|
{
|
|
if (inventoryItemPrefab != null && gameItem != null)
|
|
{
|
|
var newItem = Instantiate(inventoryItemPrefab, transform);
|
|
var inventoryItem = newItem.GetComponent<InventoryItem>();
|
|
|
|
if (inventoryItem != null) inventoryItem.SetItem(gameItem);
|
|
}
|
|
}
|
|
|
|
public void Setup(GameItem item)
|
|
{
|
|
/*itemName.text = item.itemName;
|
|
icon.sprite = item.icon;*/
|
|
}
|
|
} |