mirror of
https://github.com/DerTyp7/example-top-down-unity.git
synced 2025-10-29 12:32:09 +01:00
47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class UI_Inventory : MonoBehaviour
|
|
{
|
|
Inventory playerInventory;
|
|
public Transform inventoryContainer;
|
|
public Transform itemSlotTemplate;
|
|
private void Awake()
|
|
{
|
|
//itemSlotTemplate = inventoryContainer.Find("containerTemplate");
|
|
}
|
|
public void setInventory(Inventory inventory)
|
|
{
|
|
playerInventory = inventory;
|
|
updateInventory();
|
|
}
|
|
private void updateInventory()
|
|
{
|
|
int x = 0;
|
|
int y = 0;
|
|
float slotSize = 100;
|
|
foreach (Slot slot in playerInventory.getInventory)
|
|
{
|
|
RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, inventoryContainer).GetComponent<RectTransform>();
|
|
itemSlotRectTransform.anchoredPosition = new Vector2(x* slotSize,y * slotSize);
|
|
itemSlotRectTransform.gameObject.SetActive(true);
|
|
|
|
|
|
if (slot.ItemType != null)
|
|
{
|
|
Transform Item = itemSlotRectTransform.Find("ItemTemplate");
|
|
Item.gameObject.SetActive(true);
|
|
Item.GetComponent<Image>().sprite = slot.ItemType.sprite;
|
|
}
|
|
x++;
|
|
if (x > 3)
|
|
{
|
|
x = 0;
|
|
y--;
|
|
}
|
|
}
|
|
}
|
|
}
|