64 lines
1.5 KiB
C#
64 lines
1.5 KiB
C#
using UnityEngine;
|
|
|
|
public class InventoryUI : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private Inventory inventory;
|
|
|
|
[SerializeField]
|
|
private GameObject slotPrefab;
|
|
|
|
[SerializeField]
|
|
private Transform slotParent;
|
|
|
|
private InventorySlot[,] slots;
|
|
|
|
private void Start()
|
|
{
|
|
slots = new InventorySlot[inventory.Width, inventory.Height];
|
|
|
|
CreateGrid();
|
|
|
|
Refresh();
|
|
}
|
|
|
|
private void CreateGrid()
|
|
{
|
|
for (int y = 0; y < inventory.Height; y++)
|
|
{
|
|
for (int x = 0; x < inventory.Width; x++)
|
|
{
|
|
GameObject slotObject = Instantiate(slotPrefab, slotParent);
|
|
|
|
InventorySlot slot = slotObject.GetComponent<InventorySlot>();
|
|
|
|
slot.Initialize(x, y);
|
|
|
|
slots[x, y] = slot;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Refresh()
|
|
{
|
|
for (int x = 0; x < inventory.Width; x++)
|
|
{
|
|
for (int y = 0; y < inventory.Height; y++)
|
|
{
|
|
InventoryItemPlacement placement = inventory.GetPlacementAt(x, y);
|
|
|
|
if (placement == null)
|
|
{
|
|
slots[x, y].SetItem(null, false);
|
|
}
|
|
else
|
|
{
|
|
bool isOrigin = placement.Origin.x == x &&
|
|
placement.Origin.y == y;
|
|
|
|
slots[x, y].SetItem(placement.Item, isOrigin);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |