evelynshilosky

StorageManager - Part 33

Mar 7th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.65 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class StorageManager : MonoBehaviour
  7. {
  8.     public static StorageManager Instance { get; set; }
  9.  
  10.     [SerializeField] GameObject storageBoxSmallUI;
  11.     [SerializeField] StorageBox selectedStorage;
  12.     public bool storageUIOpen;
  13.  
  14.     private void Awake()
  15.     {
  16.         if (Instance != null && Instance != this)
  17.         {
  18.             Destroy(gameObject);
  19.         }
  20.         else
  21.         {
  22.             Instance = this;
  23.         }
  24.     }
  25.  
  26.     public void OpenBox(StorageBox storage)
  27.     {
  28.         SetSelectedStorage(storage);
  29.  
  30.         PopulateStorage(GetRelevantUI(selectedStorage));
  31.  
  32.         GetRelevantUI(selectedStorage).SetActive(true);
  33.         storageUIOpen = true;
  34.  
  35.         Cursor.lockState = CursorLockMode.None;
  36.         Cursor.visible = true;
  37.  
  38.         SelectionManager.Instance.DisableSelection();
  39.         SelectionManager.Instance.GetComponent<SelectionManager>().enabled = false;
  40.     }
  41.  
  42.     private void PopulateStorage(GameObject storageUI)
  43.     {
  44.         // Get all slots of the ui
  45.         List<GameObject> uiSlots = new List<GameObject>();
  46.  
  47.         foreach (Transform child in storageUI.transform)
  48.         {
  49.             uiSlots.Add(child.gameObject);
  50.         }
  51.  
  52.         // Now, instantiate the prefab and set it as a child of each GameObject
  53.         foreach (string name in selectedStorage.items)
  54.         {
  55.             foreach (GameObject slot in uiSlots)
  56.             {
  57.                 if (slot.transform.childCount < 1)
  58.                 {
  59.                     var itemToAdd = Instantiate(Resources.Load<GameObject>(name), slot.transform.position, slot.transform.rotation);
  60.  
  61.                     itemToAdd.name = name;
  62.                    
  63.                     itemToAdd.transform.SetParent(slot.transform);
  64.                     break;
  65.                 }
  66.             }
  67.         }
  68.     }
  69.  
  70.     public void CloseBox()
  71.     {
  72.         RecalculateStorage(GetRelevantUI(selectedStorage));
  73.  
  74.  
  75.         GetRelevantUI(selectedStorage).SetActive(false);
  76.         storageUIOpen = false;
  77.  
  78.         Cursor.lockState = CursorLockMode.Locked;
  79.         Cursor.visible = false;
  80.  
  81.         SelectionManager.Instance.EnableSelection();
  82.         SelectionManager.Instance.GetComponent<SelectionManager>().enabled = true;
  83.     }
  84.  
  85.  
  86.  
  87.     private void RecalculateStorage(GameObject storageUI)
  88.     {
  89.         // Get all slots of the UI  
  90.         List<GameObject> uiSlots = new List<GameObject>();
  91.         foreach (Transform child in storageUI.transform)
  92.         {
  93.             uiSlots.Add(child.gameObject);
  94.         }
  95.  
  96.         // Clear list of items
  97.         selectedStorage.items.Clear();
  98.  
  99.         List<GameObject> toBeDeleted = new List<GameObject>();
  100.  
  101.         // Take the inventory items and convert them into strings
  102.         foreach (GameObject slot in uiSlots)
  103.         {
  104.             if (slot.transform.childCount > 0)
  105.             {
  106.                 // Remove "Clone" text
  107.                 string name = slot.transform.GetChild(0).name;
  108.                 string str2 = "(Clone)";
  109.                 string result = name.Replace(str2, "");
  110.  
  111.                 selectedStorage.items.Add(result);
  112.                 toBeDeleted.Add(slot.transform.GetChild(0).gameObject);
  113.             }
  114.         }
  115.  
  116.         foreach (GameObject obj in toBeDeleted)
  117.         {
  118.             Destroy(obj);
  119.         }
  120.     }
  121.  
  122.  
  123.  
  124.  
  125.     public void SetSelectedStorage(StorageBox storage)
  126.     {
  127.         selectedStorage = storage;
  128.     }
  129.  
  130.     private GameObject GetRelevantUI(StorageBox storage)
  131.     {
  132.         // Create a switch for other types
  133.         return storageBoxSmallUI;
  134.     }
  135. }
  136.  
Add Comment
Please, Sign In to add comment