Files
OTG_3/Assets/_Scripts/InventoryUI.cs
T

80 lines
1.7 KiB
C#
Raw Normal View History

2026-06-04 18:31:31 +03:00
using UnityEngine;
using UnityEngine.UI;
public class InventoryUI : MonoBehaviour
{
public Inventory inventory;
public Transform slotContainer;
public GameObject slotPrefab;
private void OnEnable()
{
if (inventory != null)
{
inventory.OnInventoryChanged += RefreshUI;
}
RefreshUI();
}
private void OnDisable()
{
if (inventory != null)
{
inventory.OnInventoryChanged -= RefreshUI;
}
}
public void RefreshUI()
{
if (slotContainer == null || slotPrefab == null)
{
return;
}
ClearSlots();
if (inventory == null)
{
return;
}
for (int i = 0; i < inventory.slots.Count; i++)
{
InventorySlot slot = inventory.slots[i];
if (slot == null || slot.IsEmpty())
{
continue;
}
GameObject view = Instantiate(slotPrefab, slotContainer);
view.SetActive(true);
Text label = view.GetComponentInChildren<Text>(true);
if (label != null)
{
label.text = slot.GetDisplayText();
}
Image colorMarker = view.transform.Find("ColorMarker")?.GetComponent<Image>();
if (colorMarker != null && slot.item != null)
{
colorMarker.color = slot.item.itemColor;
}
}
}
public void ClearSlots()
{
if (slotContainer == null)
{
return;
}
for (int i = slotContainer.childCount - 1; i >= 0; i--)
{
Destroy(slotContainer.GetChild(i).gameObject);
}
}
}