mirror of
https://github.com/DerTyp7/harvestdale-unity.git
synced 2025-10-30 04:57:09 +01:00
inventory ui
This commit is contained in:
50
Assets/Scripts/UI/InventoryUI.cs
Normal file
50
Assets/Scripts/UI/InventoryUI.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class InventoryUI : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject slotPrefab;
|
||||
[SerializeField] private GameObject slotListObj;
|
||||
[SerializeField] private List<SlotUI> slotUIList;
|
||||
|
||||
private Inventory playerInventory;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
|
||||
Inventory.OnPlayerInventoryChanged += UpdateSlots;
|
||||
CreateSlots();
|
||||
UpdateSlots();
|
||||
|
||||
}
|
||||
|
||||
private void CreateSlots()
|
||||
{
|
||||
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
|
||||
|
||||
for (int i = 0; i < playerInventory.maxSlots; i++)
|
||||
{
|
||||
slotUIList.Add(Instantiate(slotPrefab, Vector3.zero, Quaternion.identity, slotListObj.transform).GetComponent<SlotUI>());
|
||||
}
|
||||
}
|
||||
private void UpdateSlots()
|
||||
{
|
||||
Debug.Log("Update slots");
|
||||
int i = 0;
|
||||
foreach (SlotUI slotUi in slotUIList)
|
||||
{
|
||||
if (i < playerInventory.items.Count)
|
||||
{
|
||||
slotUi.SetInventoryItem(playerInventory.items[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
slotUi.SetInventoryItem(null);
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/UI/InventoryUI.cs.meta
Normal file
11
Assets/Scripts/UI/InventoryUI.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9269c8c372de3f5498d2af345ee9c855
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
44
Assets/Scripts/UI/SlotUI.cs
Normal file
44
Assets/Scripts/UI/SlotUI.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class SlotUI : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private InventoryItem inventoryItem;
|
||||
[SerializeField] private Image itemSpriteImage;
|
||||
[SerializeField] private TMPro.TextMeshProUGUI quantityText;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
UpdateSlot();
|
||||
}
|
||||
public void SetInventoryItem(InventoryItem newInventoryItem)
|
||||
{
|
||||
inventoryItem = newInventoryItem;
|
||||
UpdateSlot();
|
||||
}
|
||||
|
||||
private void UpdateSlot()
|
||||
{
|
||||
if (inventoryItem != null)
|
||||
{
|
||||
itemSpriteImage.sprite = inventoryItem?.item?.sprite ?? null;
|
||||
Debug.Log(itemSpriteImage.sprite);
|
||||
if (itemSpriteImage.sprite == null)
|
||||
{
|
||||
itemSpriteImage.color = new Color(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
quantityText.SetText(inventoryItem.count != 0 ? inventoryItem.count.ToString() : "");
|
||||
}
|
||||
else
|
||||
{
|
||||
itemSpriteImage.sprite = null;
|
||||
quantityText.SetText("");
|
||||
itemSpriteImage.color = new Color(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/UI/SlotUI.cs.meta
Normal file
11
Assets/Scripts/UI/SlotUI.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6c881951842cc4e4799994f5eb91dd90
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user