evershade/Assets/Scripts/Controllers/InventoryUIController.cs

90 lines
2.7 KiB
C#

using System.Collections.Generic;
using UnityEngine;
// InventoryUI.cs
public class InventoryUIController : MonoBehaviour
{
public InventoryManager inventoryManager;
public Transform itemParent;
public GameObject itemSlotPrefab;
public GameObject inventoryItemPrefab;
public readonly Dictionary<int, GameObject> DisplayedSlots = new();
private void Start()
{
Debug.Log("InventoryUI Start");
if (itemParent == null)
{
Debug.LogWarning("ItemParent is null! Can not attach");
return;
}
// Delete existing children if any.
foreach (Transform child in itemParent) Destroy(child.gameObject);
if (inventoryManager.inventorySlots.Count == 0)
inventoryManager.InitializeInventory();
foreach (var inventorySlot in inventoryManager.inventorySlots)
{
// Draw slots to screen
var slotDisplay = Instantiate(itemSlotPrefab, itemParent);
var i = inventorySlot.slotId;
// Add prefab to slot
slotDisplay.GetComponent<InventorySlotDisplayController>().SetInventoryItemPrefab(inventoryItemPrefab);
slotDisplay.GetComponent<InventorySlotDisplayController>().SetSlotDetails(inventorySlot);
DisplayedSlots.Add(i, slotDisplay);
// GenerateInventorySlotDisplay(inventorySlot);
}
// Debug.Log("Adding item to slot:" + item.GetItemName());
Debug.Log("Finished adding items to slots");
}
private void AddItemToSlot(GameItem gameItem, int id)
{
/*Debug.Log(item);
Debug.Log(item.itemName);
ListChildren(slot);
var itemNameText = slot.GetComponentInChildren<TextMeshProUGUI>();
if (itemNameText != null)
itemNameText.text = item.itemName;
else
Debug.LogWarning("ItemName Text component not found in slot: " + slot);
// slot.GetComponentInChildren<Text>().text = item.itemName;
var iconTransform = slot.transform.Find("Icon");
if (iconTransform != null)
{
var iconImage = iconTransform.GetComponent<Image>();
if (iconImage != null)
iconImage.sprite = item.icon;
else
Debug.LogWarning("Image component not found on Icon child: " + slot);
}
else
{
Debug.LogWarning("Icon child not found in slot: " + slot);
}*/
}
public void ListChildren(GameObject parent)
{
foreach (Transform child in parent.transform) Debug.Log("Child: " + child.name);
}
public void UpdateUI()
{
// Get values from inventory and redraw the inventory slots based on that.
}
}