Files
harvestdale-unity/Assets/Scripts/UI/InventoryUI.cs
2023-02-22 13:50:33 +01:00

44 lines
1.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryUI : MonoBehaviour
{
[SerializeField] private GameObject slotPrefab;
[SerializeField] private GameObject slotListObj;
[SerializeField] private List<SlotUI> slotUIList;
private Inventory playerInventory;
private void Start()
{
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
Inventory.OnPlayerInventoryChanged += UpdateSlots;
CreateSlots();
UpdateSlots();
}
private void CreateSlots()
{
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
for (int i = 0; i < playerInventory.maxSlots; i++)
{
SlotUI newSlot = Instantiate(slotPrefab, Vector3.zero, Quaternion.identity, slotListObj.transform).GetComponent<SlotUI>();
newSlot.slotIndex = i;
slotUIList.Add(newSlot);
}
}
private void UpdateSlots()
{
int i = 0;
foreach (SlotUI slotUi in slotUIList)
{
slotUi.SetInventoryItem(playerInventory.items[i]);
i++;
}
}
}