using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; public class QuickInventoryUI : MonoBehaviour { [SerializeField] private GameObject[] uiSlots; [SerializeField] private GameObject buttonOptions; [SerializeField] private GameObject insertPanel; private GameManager gameManager; public GameObject currentButtonOptions = null; private GameObject currentInsertPanel = null; private InventoryManager inventoryManager; private Inventory inventory; void Awake() { } // Start is called before the first frame update void Start() { gameManager = GameManager.Instance; inventoryManager = gameManager.PlayerController.GetComponentInChildren<InventoryManager>(); if (inventoryManager != null) { inventory = inventoryManager.QuickInventory; Debug.Log($"Inventory: {inventory}\nInventory Capacity: {inventory.Capacity}"); //inventory.InventoryChanged += UpdateUI; inventory.InventoryChanged += UpdateSlots; } else { Debug.LogError($"Component not found: {nameof(InventoryManager)}"); } } void UpdateSlots() { for (int i = 0; i < uiSlots.Length; i++) { if (uiSlots[i].TryGetComponent(out InventoryButton button)) { button.ButtonInit(inventoryManager, i, inventoryManager.QuickInventory.GetSlot(i)); UpdateUI(i); } } } private void UpdateUI(int i) { DisplayItemUIInfo(i, uiSlots[i]); } private void DisplayItemUIInfo(int index, GameObject slot) { // Get item from inventory list if (index < inventory.Count) { InventorySlot inventorySlot = inventory.GetSlot(index); if (inventorySlot != null) { // Assign item sprite to the UI slot image Image[] images = slot.GetComponentsInChildren<Image>(); TextMeshProUGUI tmp = slot.GetComponentInChildren<TextMeshProUGUI>(); if (images != null) { foreach (Image image in images) { if (image.gameObject.transform.childCount == 0) { image.color = Color.white; image.sprite = inventorySlot.Item.ItemImage; tmp.text = inventorySlot.Amount > 1 ? inventorySlot.Amount.ToString() : string.Empty; } } Debug.Log($"Changed Sprite at index: {index}"); return; } } Debug.LogWarning("Item is NUll"); } } public void DisplayButtonOptions(Vector3 position, int index) { if (currentButtonOptions != null) Destroy(currentButtonOptions); currentButtonOptions = Instantiate(buttonOptions, position, Quaternion.identity, gameObject.transform); Button[] buttons = currentButtonOptions.GetComponentsInChildren<Button>(); buttons[0].onClick.AddListener(delegate { SplitItemStack(index); Destroy(currentButtonOptions); }); buttons[1].onClick.AddListener(delegate { //CreateInsertPanel(index); Destroy(currentButtonOptions); }); buttons[2].onClick.AddListener(delegate { //Discard the item DiscardItem(index); Destroy(currentButtonOptions); }); } public void SplitItemStack(int index) => inventory.SplitSlot(index); public void DiscardItem(int index) => inventory.RemoveItem(index); public void Receive(int value, int index) => inventoryManager.DropItem(index, value); private void OnEnable() { if (inventory != null) { // Subscribe to inventory changes when the UI is active inventory.InventoryChanged += UpdateSlots; } } private void OnDisable() { inventory.InventoryChanged -= UpdateSlots; } }