evelynshilosky

UIManager - Part 6.1

Mar 27th, 2025
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.82 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.EventSystems;
  4. using System.Collections.Generic;
  5. using TMPro;
  6.  
  7. public class UIManager : MonoBehaviour
  8. {
  9.     public static UIManager Instance { get; private set; }
  10.  
  11.     public GameObject inventoryPanel;
  12.     public GameObject inventoryPrompt;
  13.     public Text promptText;
  14.     public Button yesButton;
  15.     public Button noButton;
  16.     public Button okButton;
  17.     public Button closeButton;
  18.     public Button inventoryDoneButton;
  19.     public Transform inventorySlotParent;
  20.     public Transform storageHolder;
  21.     public GameObject moneyDisplay;
  22.     public GameObject doneButton;
  23.     public GameObject itemDetailsPanel;
  24.     public TextMeshProUGUI itemTitleText;
  25.     public TextMeshProUGUI itemDescriptionText;
  26.     public TextMeshProUGUI itemUsesText;
  27.     public TextMeshProUGUI inventoryTitleText;
  28.  
  29.     [SerializeField] private GameObject itemIconPrefab;
  30.  
  31.     private InventorySystem inventorySystem;
  32.     private StorageSystem storageSystem;
  33.     private InteractionSystem interactionSystem;
  34.     private PlayerMovement playerMovement;
  35.     private MouseMovement mouseMovement;
  36.     private Item currentStorageItem;
  37.     private Item draggedItem;
  38.     private Transform originalSlot;
  39.  
  40.     public bool isInventoryOpen = false;
  41.  
  42.     private void Awake()
  43.     {
  44.         if (Instance == null)
  45.         {
  46.             Instance = this;
  47.         }
  48.         else
  49.         {
  50.             Destroy(gameObject);
  51.         }
  52.     }
  53.  
  54.     private void Start()
  55.     {
  56.         inventorySystem = InventorySystem.Instance;
  57.         storageSystem = StorageSystem.Instance;
  58.         interactionSystem = FindObjectOfType<InteractionSystem>();
  59.         playerMovement = FindObjectOfType<PlayerMovement>();
  60.         mouseMovement = FindObjectOfType<MouseMovement>();
  61.  
  62.         if (closeButton != null) closeButton.onClick.AddListener(CloseInventory);
  63.         if (okButton != null)
  64.         {
  65.             okButton.onClick.AddListener(ClosePrompt);
  66.             okButton.gameObject.SetActive(false);
  67.         }
  68.         if (yesButton != null) yesButton.onClick.AddListener(() => HandleBackpackPrompt(true));
  69.         if (noButton != null) noButton.onClick.AddListener(() => HandleBackpackPrompt(false));
  70.         if (inventoryDoneButton != null) inventoryDoneButton.onClick.AddListener(CloseInventory);
  71.     }
  72.  
  73.     public void ShowInventoryPrompt(Item storageItem)
  74.     {
  75.         currentStorageItem = storageItem;
  76.         inventoryPrompt.SetActive(true);
  77.         promptText.text = storageItem != null ? $"Open {storageItem.itemName}?" : "";
  78.         TogglePlayerControls(false);
  79.     }
  80.  
  81.     public void UpdatePromptText(string message)
  82.     {
  83.         if (promptText != null)
  84.         {
  85.             promptText.text = message;
  86.         }
  87.     }
  88.  
  89.     public void ShowOkayButton()
  90.     {
  91.         if (okButton != null)
  92.         {
  93.             okButton.gameObject.SetActive(true);
  94.             yesButton.gameObject.SetActive(false);
  95.             noButton.gameObject.SetActive(false);
  96.         }
  97.     }
  98.  
  99.     public void OpenInventory(Item storageItem)
  100.     {
  101.         if (storageItem.isBackpack)
  102.         {
  103.             return;
  104.         }
  105.  
  106.         inventoryPrompt.SetActive(false);
  107.         inventoryPanel.SetActive(true);
  108.         currentStorageItem = storageItem;
  109.         isInventoryOpen = true;
  110.  
  111.         TransferHeldItemsToStorage();
  112.         PopulateInventoryUI(storageItem);
  113.         TogglePlayerControls(false);
  114.  
  115.         if (inventoryTitleText != null)
  116.         {
  117.             inventoryTitleText.text = storageItem.itemName;
  118.         }
  119.     }
  120.  
  121.     private void TransferHeldItemsToStorage()
  122.     {
  123.         if (inventorySystem.leftHandItem != null)
  124.         {
  125.             bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.leftHandItem);
  126.             if (added)
  127.             {
  128.                 inventorySystem.UnequipItem(true);
  129.                 playerMovement.UpdateCarryingAnimations();
  130.             }
  131.         }
  132.         if (inventorySystem.rightHandItem != null)
  133.         {
  134.             bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.rightHandItem);
  135.             if (added)
  136.             {
  137.                 inventorySystem.UnequipItem(false);
  138.                 playerMovement.UpdateCarryingAnimations();
  139.             }
  140.         }
  141.     }
  142.  
  143.     public void PopulateInventoryUI(Item storageItem)
  144.     {
  145.         UIHelperFunctions.Instance.PopulateInventoryUI(storageItem, inventorySlotParent, storageSystem, this);
  146.     }
  147.  
  148.     public void UpdateInventorySlot(GameObject slotObject, Item item)
  149.     {
  150.         UIHelperFunctions.Instance.UpdateInventorySlot(slotObject, item, itemIconPrefab, this);
  151.     }
  152.  
  153.     public void ShowItemDetails(Item item)
  154.     {
  155.         UIHelperFunctions.Instance.ShowItemDetails(item, itemDetailsPanel, itemTitleText, itemDescriptionText, itemUsesText);
  156.     }
  157.  
  158.     public void HideItemDetails()
  159.     {
  160.         UIHelperFunctions.Instance.HideItemDetails(itemDetailsPanel);
  161.     }
  162.  
  163.     public void TryEquipItem(Item item)
  164.     {
  165.         UIHelperFunctions.Instance.TryEquipItem(item, inventorySystem, storageSystem, currentStorageItem, interactionSystem, playerMovement, this);
  166.     }
  167.  
  168.     public void BeginDrag(Transform iconTransform)
  169.     {
  170.         UIHelperFunctions.Instance.BeginDrag(iconTransform, ref draggedItem, ref originalSlot, inventoryPanel);
  171.     }
  172.  
  173.     public void EndDrag(Transform iconTransform)
  174.     {
  175.         UIHelperFunctions.Instance.EndDrag(iconTransform, draggedItem, originalSlot, inventorySlotParent, currentStorageItem, storageSystem, this);
  176.         draggedItem = null;
  177.     }
  178.  
  179.     public void ShowError(string message)
  180.     {
  181.         promptText.text = message;
  182.         yesButton.gameObject.SetActive(false);
  183.         noButton.gameObject.SetActive(false);
  184.         okButton.gameObject.SetActive(true);
  185.     }
  186.  
  187.     public void CloseInventory()
  188.     {
  189.         inventoryPanel.SetActive(false);
  190.         isInventoryOpen = false;
  191.         TogglePlayerControls(true);
  192.     }
  193.  
  194.     private void TogglePlayerControls(bool enable)
  195.     {
  196.         if (playerMovement != null) playerMovement.enabled = enable;
  197.         if (mouseMovement != null) mouseMovement.enabled = enable;
  198.         SetCursorState(!enable);
  199.     }
  200.  
  201.     public void ShowBackpackPrompt(Item backpackItem)
  202.     {
  203.         currentStorageItem = backpackItem;
  204.         inventoryPrompt.SetActive(true);
  205.         promptText.text = "Would you like to wear the backpack?";
  206.         yesButton.gameObject.SetActive(true);
  207.         noButton.gameObject.SetActive(true);
  208.         okButton.gameObject.SetActive(false);
  209.         SetCursorState(true);
  210.         FreezePlayerMovement(true);
  211.     }
  212.  
  213.     public void HandleBackpackPrompt(bool accepted)
  214.     {
  215.         if (accepted)
  216.         {
  217.             interactionSystem.WearBackpack();
  218.         }
  219.         else
  220.         {
  221.             UpdateBackpackPrompt();
  222.         }
  223.     }
  224.  
  225.     public void UpdateBackpackPrompt()
  226.     {
  227.         promptText.text = "Okay, if you change your mind press E to equip the backpack";
  228.         yesButton.gameObject.SetActive(false);
  229.         noButton.gameObject.SetActive(false);
  230.         okButton.gameObject.SetActive(true);
  231.         SetCursorState(true);
  232.         FreezePlayerMovement(true);
  233.     }
  234.  
  235.     public void ClosePrompt()
  236.     {
  237.         inventoryPrompt.SetActive(false);
  238.         SetCursorState(false);
  239.         FreezePlayerMovement(false);
  240.         currentStorageItem = null;
  241.     }
  242.  
  243.     private void SetCursorState(bool visible)
  244.     {
  245.         Cursor.visible = visible;
  246.         Cursor.lockState = visible ? CursorLockMode.None : CursorLockMode.Locked;
  247.     }
  248.  
  249.     private void FreezePlayerMovement(bool freeze)
  250.     {
  251.         if (playerMovement != null) playerMovement.enabled = !freeze;
  252.         if (mouseMovement != null) mouseMovement.enabled = !freeze;
  253.     }
  254. }
  255.  
  256. public class ItemReference : MonoBehaviour
  257. {
  258.     public Item item;
  259. }
  260.  
Advertisement
Add Comment
Please, Sign In to add comment