80 lines
1.7 KiB
C#
80 lines
1.7 KiB
C#
|
|
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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|