Some checks failed
Build project / Build for (StandaloneWindows64, 6000.0.37f1) (push) Has been cancelled
Build project / Publish to itch.io (StandaloneLinux64) (push) Has been cancelled
Build project / Publish to itch.io (StandaloneWindows64) (push) Has been cancelled
Build project / Build for (StandaloneLinux64, 6000.0.37f1) (push) Has been cancelled
59 lines
1.8 KiB
C#
59 lines
1.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class InventoryUI : MonoBehaviour
|
|
{
|
|
[SerializeField] private Image[] slotImages; // 10 UI Images
|
|
[SerializeField] private Inventory inventory; // The fixed-slot Inventory
|
|
|
|
// These let you pick highlight/normal colors in the Inspector
|
|
[SerializeField] private Color normalColor = Color.white;
|
|
[SerializeField] private Color highlightColor = Color.yellow;
|
|
|
|
void Start()
|
|
{
|
|
RefreshUI();
|
|
}
|
|
|
|
public void RefreshUI()
|
|
{
|
|
for (int i = 0; i < slotImages.Length; i++)
|
|
{
|
|
Item itemInSlot = inventory.slots[i];
|
|
if (itemInSlot != null && itemInSlot.itemIcon != null)
|
|
{
|
|
slotImages[i].sprite = itemInSlot.itemIcon;
|
|
slotImages[i].color = normalColor;
|
|
}
|
|
else
|
|
{
|
|
slotImages[i].sprite = null;
|
|
slotImages[i].color = new Color(1,1,1,0); // or Color.clear
|
|
}
|
|
}
|
|
|
|
HighlightSlot(inventory.currentSlot); // after we set sprites, highlight the active slot
|
|
}
|
|
|
|
private void HighlightSlot(int slotIndex)
|
|
{
|
|
// Make sure it's a valid index
|
|
if (slotIndex < 0 || slotIndex >= slotImages.Length)
|
|
return;
|
|
|
|
// Set all slots to normal color
|
|
for (int i = 0; i < slotImages.Length; i++)
|
|
{
|
|
// If the slot is empty, you might want to leave it "clear"
|
|
// but let's just do normalColor for demonstration:
|
|
if (slotImages[i].sprite == null)
|
|
slotImages[i].color = new Color(1,1,1,0); // keep empty slot invisible
|
|
else
|
|
slotImages[i].color = normalColor;
|
|
}
|
|
|
|
// Now highlight the active slot
|
|
slotImages[slotIndex].color = highlightColor;
|
|
}
|
|
}
|