evershade/Assets/Scripts/UI/InventoryUI.cs

87 lines
2.6 KiB
C#

using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
// InventoryUI.cs
public class InventoryUI : MonoBehaviour
{
public InventoryManager inventoryManager;
public Transform itemParent;
public GameObject itemSlotPrefab;
public GameObject inventoryItemPrefab;
public int maxSlots = 30;
public readonly Dictionary<int, GameObject> slots = new();
private void Start()
{
Debug.Log("InventoryUI Start");
if (itemParent == null)
{
Debug.LogWarning("ItemParent is null!");
return;
}
foreach (Transform child in itemParent) Destroy(child.gameObject);
Debug.Log("Finished deleting old crap");
for (var i = 0; i < maxSlots; i++)
{
var slot = Instantiate(itemSlotPrefab, itemParent);
Debug.Log("created slot");
slots.Add(i, slot);
}
// foreach (var item in inventoryManager.inventoryItems) addItemToSlot(slot, item);
}
private void addItemToSlot(GameObject slot, GameItem item)
{
Debug.Log(item);
Debug.Log(item.itemName);
ListChildren(slot);
var itemNameText = slot.GetComponentInChildren<TextMeshProUGUI>();
if (itemNameText != null)
itemNameText.text = item.itemName;
else
Debug.LogWarning("ItemName Text component not found in slot: " + slot);
// slot.GetComponentInChildren<Text>().text = item.itemName;
var iconTransform = slot.transform.Find("Icon");
if (iconTransform != null)
{
var iconImage = iconTransform.GetComponent<Image>();
if (iconImage != null)
iconImage.sprite = item.icon;
else
Debug.LogWarning("Image component not found on Icon child: " + slot);
}
else
{
Debug.LogWarning("Icon child not found in slot: " + slot);
}
}
public void ListChildren(GameObject parent)
{
foreach (Transform child in parent.transform) Debug.Log("Child: " + child.name);
}
public void UpdateUI()
{
foreach (Transform child in itemParent) Destroy(child.gameObject);
foreach (var item in inventoryManager.inventoryItems)
{
var slot = Instantiate(itemSlotPrefab, itemParent);
slot.GetComponentInChildren<Text>().text = item.itemName;
// slot.GetComponentInChildren<Image>().sprite = item.icon;
}
}
}