91 lines
2.8 KiB
C#
91 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
|
|
public class UI_Inventory : MonoBehaviour
|
|
{
|
|
private Inventory inventory;
|
|
private Transform itemSlotContainer;
|
|
private Transform itemSlotTemplate;
|
|
private List<RectTransform> itemSlots = new List<RectTransform>();
|
|
public Text Amount;
|
|
|
|
private void Awake()
|
|
{
|
|
itemSlotContainer = transform.Find("itemSlotContainer");
|
|
itemSlotTemplate = itemSlotContainer.Find("itemSlotTemplate");
|
|
}
|
|
|
|
public void SetInventory(Inventory inventory)
|
|
{
|
|
this.inventory = inventory;
|
|
RefreshInventoryItems();
|
|
}
|
|
|
|
//public void UpdateInventory()
|
|
//{
|
|
// Iteruj przez sloty od koñca do pocz¹tku, aby unikaæ problemów z indeksami
|
|
//for (int i = itemSlots.Count - 1; i >= 0; i--)
|
|
// {
|
|
// RectTransform slot = itemSlots[i];
|
|
// if (slot != null)
|
|
// {
|
|
// SprawdŸ, czy slot ma komponent Image i Text, które przechowuj¹ dane o przedmiotach
|
|
// Image image = slot.Find("Image").GetComponent<Image>();
|
|
// Text text = slot.Find("Amount").GetComponent<Text>();
|
|
|
|
// if (image != null && text != null)
|
|
// {
|
|
// Zak³adam, ¿e przedmiot jest powi¹zany z obrazem i tekstem w slocie
|
|
// Sprite itemSprite = image.sprite;
|
|
//int itemAmount = int.Parse(text.text);
|
|
|
|
// Funkcja powinna zwróciæ Item na podstawie sprite
|
|
|
|
// }
|
|
// }
|
|
// }
|
|
//}
|
|
|
|
public void RefreshInventoryItems()
|
|
{
|
|
|
|
|
|
int x = 0;
|
|
int y = 0;
|
|
float itemSlotCellSize = 30f;
|
|
|
|
foreach (Item item in inventory.GetItemList())
|
|
{
|
|
RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, itemSlotContainer).GetComponent<RectTransform>();
|
|
itemSlotRectTransform.gameObject.SetActive(true);
|
|
itemSlotRectTransform.anchoredPosition = new Vector2(x * itemSlotCellSize, y * itemSlotCellSize);
|
|
|
|
Image image = itemSlotRectTransform.Find("Image").GetComponent<Image>();
|
|
Text text = itemSlotRectTransform.Find("Amount").GetComponent<Text>();
|
|
|
|
image.sprite = item.GetSprite();
|
|
text.text = item.amount.ToString();
|
|
|
|
itemSlots.Add(itemSlotRectTransform);
|
|
|
|
x++;
|
|
if (x > 1) // Poprawi³em warunek na wiêkszy, aby dostosowaæ pozycjonowanie
|
|
{
|
|
x = 0;
|
|
y++;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ClearInventoryItems()
|
|
{
|
|
foreach (RectTransform slot in itemSlots)
|
|
{
|
|
Destroy(slot.gameObject);
|
|
}
|
|
itemSlots.Clear();
|
|
}
|
|
} |