71 lines
1.9 KiB
C#
71 lines
1.9 KiB
C#
using System.Collections.Generic;
|
|
using Managers;
|
|
using UnityEngine;
|
|
|
|
// InventoryUI.cs
|
|
|
|
public class InventoryUIController : MonoBehaviour
|
|
{
|
|
public Transform itemParent;
|
|
|
|
private readonly Dictionary<int, GameObject> _slots = new();
|
|
public static InventoryUIController Instance { get; private set; }
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
Debug.Log("InventoryUI Start");
|
|
if (itemParent == null) Debug.LogWarning("ItemParent is null! Can not attach");
|
|
UpdateUI();
|
|
}
|
|
|
|
|
|
public void ListChildren(GameObject parent)
|
|
{
|
|
foreach (Transform child in parent.transform) Debug.Log("Child: " + child.name);
|
|
}
|
|
|
|
public void UpdateUI()
|
|
{
|
|
// Clear existing slots
|
|
_slots.Clear();
|
|
// Get values from inventory and redraw the inventory slots based on that.
|
|
// Delete existing children if any.
|
|
foreach (Transform child in itemParent) Destroy(child.gameObject);
|
|
|
|
if (InventoryManager.Instance.Slots.Count == 0)
|
|
InventoryManager.Instance.Initialize(PlayerManager.Instance.playerData);
|
|
|
|
var slotPrefab = PrefabManager.Instance.GetSlotPrefab();
|
|
var itemPrefab = PrefabManager.Instance.GetItemPrefab();
|
|
|
|
|
|
foreach (var inventorySlot in InventoryManager.Instance.Slots)
|
|
{
|
|
// Draw slots to screen
|
|
var slotDisplay = Instantiate(slotPrefab, itemParent);
|
|
var i = inventorySlot.Key;
|
|
|
|
var inventorySlotComponent = slotDisplay.GetComponent<SlotComponent>();
|
|
|
|
// Add prefab to slot
|
|
inventorySlotComponent.SetSlotDetails(inventorySlot.Value);
|
|
|
|
_slots.Add(i, slotDisplay);
|
|
}
|
|
|
|
Debug.Log("Finished adding items to slots");
|
|
}
|
|
} |