73 lines
2.3 KiB
C#
73 lines
2.3 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
// InventoryUI.cs
|
|
|
|
public class InventoryUI : MonoBehaviour
|
|
{
|
|
public InventoryManager inventoryManager;
|
|
public Transform itemParent;
|
|
public GameObject itemSlotPrefab;
|
|
|
|
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");
|
|
foreach (var item in inventoryManager.inventoryItems)
|
|
{
|
|
var slot = Instantiate(itemSlotPrefab, itemParent);
|
|
Debug.Log("created slot");
|
|
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;
|
|
}
|
|
}
|
|
} |