Advertisement
evelynshilosky

UIManager - Part 1

Jan 21st, 2025
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.28 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections.Generic;
  4.  
  5. public class UIManager : MonoBehaviour
  6. {
  7.     public static UIManager Instance { get; private set; }
  8.  
  9.     public GameObject inventoryPanel;
  10.     public GameObject inventoryPrompt;
  11.     public Text promptText;
  12.     public Button yesButton;
  13.     public Button noButton;
  14.     public Button closeButton;
  15.     public Transform inventorySlotParent;
  16.  
  17.     private InventorySystem inventorySystem;
  18.     private Item currentStorageItem;
  19.     private StorageSystem storageSystem;
  20.  
  21.     private void Awake()
  22.     {
  23.         if (Instance == null)
  24.         {
  25.             Instance = this;
  26.         }
  27.         else if (Instance != this)
  28.         {
  29.             gameObject.SetActive(false);
  30.             return;
  31.         }
  32.     }
  33.  
  34.     private void Start()
  35.     {
  36.         inventorySystem = InventorySystem.Instance;
  37.         storageSystem = StorageSystem.Instance;
  38.         closeButton.onClick.AddListener(CloseInventory);
  39.     }
  40.  
  41.     public void ShowInventoryPrompt(Item storageItem)
  42.     {
  43.         if (storageItem == null)
  44.         {
  45.             Debug.LogError("<color=red>Attempted to show inventory prompt for null storage item.</color>");
  46.             return;
  47.         }
  48.  
  49.         inventoryPrompt.SetActive(true);
  50.         promptText.text = $"Open {storageItem.itemName}?";
  51.         yesButton.onClick.RemoveAllListeners();
  52.         yesButton.onClick.AddListener(() => OpenInventory(storageItem));
  53.         noButton.onClick.RemoveAllListeners();
  54.         noButton.onClick.AddListener(ClosePrompt);
  55.     }
  56.  
  57.     private void OpenInventory(Item storageItem)
  58.     {
  59.         inventoryPrompt.SetActive(false);
  60.         inventoryPanel.SetActive(true);
  61.         currentStorageItem = storageItem;
  62.         PopulateInventoryUI(storageItem);
  63.         Debug.Log($"<color=green>Opened inventory for {storageItem.itemName}</color>");
  64.     }
  65.  
  66.     private void PopulateInventoryUI(Item storageItem)
  67.     {
  68.         ClearInventoryUI();
  69.         List<Item> storageInventory = storageSystem.GetStorageInventory(storageItem);
  70.         for (int i = 0; i < inventorySlotParent.childCount; i++)
  71.         {
  72.             if (i < storageInventory.Count)
  73.             {
  74.                 UpdateInventorySlot(inventorySlotParent.GetChild(i).gameObject, storageInventory[i]);
  75.             }
  76.             else
  77.             {
  78.                 ClearInventorySlot(inventorySlotParent.GetChild(i).gameObject);
  79.             }
  80.         }
  81.     }
  82.  
  83.     private void UpdateInventorySlot(GameObject slotObject, Item item)
  84.     {
  85.         Image iconImage = slotObject.GetComponentInChildren<Image>();
  86.         iconImage.sprite = item.icon;
  87.         iconImage.enabled = true;
  88.         Button slotButton = slotObject.GetComponent<Button>();
  89.         slotButton.onClick.RemoveAllListeners();
  90.         slotButton.onClick.AddListener(() => EquipItemFromInventory(item));
  91.     }
  92.  
  93.     private void ClearInventorySlot(GameObject slotObject)
  94.     {
  95.         Image iconImage = slotObject.GetComponentInChildren<Image>();
  96.         iconImage.sprite = null;
  97.         iconImage.enabled = false;
  98.         Button slotButton = slotObject.GetComponent<Button>();
  99.         slotButton.onClick.RemoveAllListeners();
  100.     }
  101.  
  102.     private void EquipItemFromInventory(Item item)
  103.     {
  104.         if (inventorySystem.leftHandItem == null)
  105.         {
  106.             inventorySystem.EquipItem(item, true, item.isTwoHanded);
  107.         }
  108.         else if (inventorySystem.rightHandItem == null && !item.isTwoHanded)
  109.         {
  110.             inventorySystem.EquipItem(item, false, false);
  111.         }
  112.         else
  113.         {
  114.             Debug.Log("<color=orange>Cannot equip item. Hands are full.</color>");
  115.             return;
  116.         }
  117.  
  118.         inventorySystem.RemoveItemFromStorage(currentStorageItem, item);
  119.         PopulateInventoryUI(currentStorageItem);
  120.         Debug.Log($"<color=green>Equipped {item.itemName} from inventory</color>");
  121.     }
  122.  
  123.     private void ClearInventoryUI()
  124.     {
  125.         foreach (Transform child in inventorySlotParent)
  126.         {
  127.             ClearInventorySlot(child.gameObject);
  128.         }
  129.     }
  130.  
  131.     private void ClosePrompt()
  132.     {
  133.         inventoryPrompt.SetActive(false);
  134.     }
  135.  
  136.     private void CloseInventory()
  137.     {
  138.         inventoryPanel.SetActive(false);
  139.         currentStorageItem = null;
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement