Advertisement
evelynshilosky

UIManager - Part 6.2.1.1

Apr 8th, 2025
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.23 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(() => HandleStoragePrompt(true)); }
  69.         if (noButton != null) noButton.onClick.AddListener(() => HandleStoragePrompt(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.         yesButton.gameObject.SetActive(true);
  79.         noButton.gameObject.SetActive(true);
  80.         okButton.gameObject.SetActive(false);
  81.         TogglePlayerControls(false);
  82.     }
  83.  
  84.     public void HandleStoragePrompt(bool accepted)
  85.     {
  86.         if (accepted && currentStorageItem != null)
  87.         {
  88.             OpenInventory(currentStorageItem); // Open inventory for the selected storage item
  89.         }
  90.         else
  91.         {
  92.             ClosePrompt(); // Close the prompt if "No" is selected
  93.         }
  94.     }
  95.  
  96.  
  97.     public void UpdatePromptText(string message)
  98.     {
  99.         if (promptText != null)
  100.         {
  101.             promptText.text = message;
  102.         }
  103.     }
  104.  
  105.     public void ShowOkayButton()
  106.     {
  107.         if (okButton != null)
  108.         {
  109.             okButton.gameObject.SetActive(true);
  110.             yesButton.gameObject.SetActive(false);
  111.             noButton.gameObject.SetActive(false);
  112.         }
  113.     }
  114.  
  115.     public void OpenInventory(Item storageItem)
  116.     {
  117.        
  118.         inventoryPrompt.SetActive(false);
  119.         inventoryPanel.SetActive(true);
  120.  
  121.         currentStorageItem = storageItem;
  122.         isInventoryOpen = true;
  123.  
  124.         TransferHeldItemsToStorage();
  125.         PopulateInventoryUI(storageItem);
  126.         TogglePlayerControls(false);
  127.  
  128.         if (inventoryTitleText != null)
  129.         {
  130.             inventoryTitleText.text = storageItem.itemName;
  131.         }
  132.     }
  133.  
  134.     private void TransferHeldItemsToStorage()
  135.     {
  136.         if (inventorySystem.leftHandItem != null)
  137.         {
  138.             bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.leftHandItem);
  139.             if (added)
  140.             {
  141.                 inventorySystem.UnequipItem(true);
  142.                 playerMovement.UpdateCarryingAnimations();
  143.             }
  144.         }
  145.         if (inventorySystem.rightHandItem != null)
  146.         {
  147.             bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.rightHandItem);
  148.             if (added)
  149.             {
  150.                 inventorySystem.UnequipItem(false);
  151.                 playerMovement.UpdateCarryingAnimations();
  152.             }
  153.         }
  154.     }
  155.  
  156.     public void PopulateInventoryUI(Item storageItem)
  157.     {
  158.         UIHelperFunctions.Instance.PopulateInventoryUI(storageItem, inventorySlotParent, storageSystem, this);
  159.     }
  160.  
  161.     public void UpdateInventorySlot(GameObject slotObject, Item item)
  162.     {
  163.         UIHelperFunctions.Instance.UpdateInventorySlot(slotObject, item, itemIconPrefab, this);
  164.     }
  165.  
  166.     public void ShowItemDetails(Item item)
  167.     {
  168.         UIHelperFunctions.Instance.ShowItemDetails(item, itemDetailsPanel, itemTitleText, itemDescriptionText, itemUsesText);
  169.     }
  170.  
  171.     public void HideItemDetails()
  172.     {
  173.         UIHelperFunctions.Instance.HideItemDetails(itemDetailsPanel);
  174.     }
  175.  
  176.     public void TryEquipItem(Item item)
  177.     {
  178.         UIHelperFunctions.Instance.TryEquipItem(item, inventorySystem, storageSystem, currentStorageItem, interactionSystem, playerMovement, this);
  179.     }
  180.  
  181.     public void BeginDrag(Transform iconTransform)
  182.     {
  183.         UIHelperFunctions.Instance.BeginDrag(iconTransform, ref draggedItem, ref originalSlot, inventoryPanel);
  184.     }
  185.  
  186.     public void EndDrag(Transform iconTransform)
  187.     {
  188.         UIHelperFunctions.Instance.EndDrag(iconTransform, draggedItem, originalSlot, inventorySlotParent, currentStorageItem, storageSystem, this);
  189.         draggedItem = null;
  190.     }
  191.  
  192.     public void ShowError(string message)
  193.     {
  194.         promptText.text = message;
  195.         yesButton.gameObject.SetActive(false);
  196.         noButton.gameObject.SetActive(false);
  197.         okButton.gameObject.SetActive(true);
  198.     }
  199.  
  200.     public void CloseInventory()
  201.     {
  202.         inventoryPanel.SetActive(false);
  203.         isInventoryOpen = false;
  204.         TogglePlayerControls(true);
  205.     }
  206.  
  207.     private void TogglePlayerControls(bool enable)
  208.     {
  209.         if (playerMovement != null) playerMovement.enabled = enable;
  210.         if (mouseMovement != null) mouseMovement.enabled = enable;
  211.         SetCursorState(!enable);
  212.     }
  213.  
  214.     public void ShowBackpackPrompt(Item backpackItem)
  215.     {
  216.         currentStorageItem = backpackItem;
  217.         inventoryPrompt.SetActive(true);
  218.         promptText.text = "Would you like to wear the backpack?";
  219.         yesButton.gameObject.SetActive(true);
  220.         noButton.gameObject.SetActive(true);
  221.         okButton.gameObject.SetActive(false);
  222.         SetCursorState(true);
  223.         FreezePlayerMovement(true);
  224.     }
  225.  
  226.     public void HandleBackpackPrompt(bool accepted)
  227.     {
  228.         if (accepted)
  229.         {
  230.             interactionSystem.WearBackpack();
  231.         }
  232.         else
  233.         {
  234.             UpdateBackpackPrompt();
  235.         }
  236.     }
  237.  
  238.     public void UpdateBackpackPrompt()
  239.     {
  240.         promptText.text = "Okay, if you change your mind press E to equip the backpack";
  241.         yesButton.gameObject.SetActive(false);
  242.         noButton.gameObject.SetActive(false);
  243.         okButton.gameObject.SetActive(true);
  244.         SetCursorState(true);
  245.         FreezePlayerMovement(true);
  246.     }
  247.  
  248.     public void ClosePrompt()
  249.     {
  250.         inventoryPrompt.SetActive(false);
  251.         SetCursorState(false);
  252.         FreezePlayerMovement(false);
  253.         currentStorageItem = null;
  254.     }
  255.  
  256.     private void SetCursorState(bool visible)
  257.     {
  258.         Cursor.visible = visible;
  259.         Cursor.lockState = visible ? CursorLockMode.None : CursorLockMode.Locked;
  260.     }
  261.  
  262.     private void FreezePlayerMovement(bool freeze)
  263.     {
  264.         if (playerMovement != null) playerMovement.enabled = !freeze;
  265.         if (mouseMovement != null) mouseMovement.enabled = !freeze;
  266.     }
  267. }
  268.  
  269. public class ItemReference : MonoBehaviour
  270. {
  271.     public Item item;
  272. }
  273.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement