mirror of
https://github.com/DerTyp7/example-top-down-unity.git
synced 2025-10-30 12:57:08 +01:00
-Added Items
-Added item stacking -adding and removing items
This commit is contained in:
46
Assets/Scripts/Inventory/UI_Inventory.cs
Normal file
46
Assets/Scripts/Inventory/UI_Inventory.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
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--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user