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<6B>ca do pocz<63>tku, aby unika<6B> problem<65>w z indeksami
|
|||
|
//for (int i = itemSlots.Count - 1; i >= 0; i--)
|
|||
|
// {
|
|||
|
// RectTransform slot = itemSlots[i];
|
|||
|
// if (slot != null)
|
|||
|
// {
|
|||
|
// Sprawd<77>, czy slot ma komponent Image i Text, kt<6B>re przechowuj<75> dane o przedmiotach
|
|||
|
// Image image = slot.Find("Image").GetComponent<Image>();
|
|||
|
// Text text = slot.Find("Amount").GetComponent<Text>();
|
|||
|
|
|||
|
// if (image != null && text != null)
|
|||
|
// {
|
|||
|
// Zak<61>adam, <20>e przedmiot jest powi<77>zany z obrazem i tekstem w slocie
|
|||
|
// Sprite itemSprite = image.sprite;
|
|||
|
//int itemAmount = int.Parse(text.text);
|
|||
|
|
|||
|
// Funkcja powinna zwr<77>ci<63> 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<77>em warunek na wi<77>kszy, aby dostosowa<77> pozycjonowanie
|
|||
|
{
|
|||
|
x = 0;
|
|||
|
y++;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void ClearInventoryItems()
|
|||
|
{
|
|||
|
foreach (RectTransform slot in itemSlots)
|
|||
|
{
|
|||
|
Destroy(slot.gameObject);
|
|||
|
}
|
|||
|
itemSlots.Clear();
|
|||
|
}
|
|||
|
}
|