Guest User

CustomInventory

a guest
Jan 30th, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.75 KB | None | 0 0
  1. using UnityEngine;
  2. using AC;
  3.  
  4. namespace AC.CombatExample
  5. {
  6.     public class CustomInventory : MonoBehaviour
  7.     {
  8.         #region Variables
  9.  
  10.         [SerializeField] public int currentlySelectedItemID = 0;
  11.         [SerializeField] private float inputDelay = 0.2f; // Delay before accepting input
  12.  
  13.         private Menu inventoryMenu;
  14.         private Menu interactionMenu;
  15.         private InventoryItemGO[] spawnedItems;
  16.         private bool isOn;
  17.         private float inputCooldown;
  18.  
  19.         #endregion
  20.  
  21.         #region UnityStandards
  22.  
  23.         private void OnEnable()
  24.         {
  25.             EventManager.OnMenuTurnOn += OnMenuTurnOn;
  26.             EventManager.OnMenuTurnOff += OnMenuTurnOff;
  27.         }
  28.  
  29.         private void OnDisable()
  30.         {
  31.             EventManager.OnMenuTurnOn -= OnMenuTurnOn;
  32.             EventManager.OnMenuTurnOff -= OnMenuTurnOff;
  33.         }
  34.  
  35.         private void Start()
  36.         {
  37.             inventoryMenu = PlayerMenus.GetMenuWithName("Inventory");
  38.             interactionMenu = PlayerMenus.GetMenuWithName("Interaction");
  39.         }
  40.  
  41.         private void Update()
  42.         {
  43.             Menu interactionsMenu = PlayerMenus.GetMenuWithName("Interaction");
  44.             inputCooldown -= Time.deltaTime; // Reduce cooldown over time
  45.             bool inventoryOn = AC.PlayerMenus.GetMenuWithName("Inventory").IsOn();
  46.             bool interactionOn = AC.PlayerMenus.GetMenuWithName("Interaction").IsOn();
  47.  
  48.             if (interactionsMenu != null)
  49.             {
  50.                 InvInstance foundInstance = GetInventoryInstanceByID(currentlySelectedItemID);
  51.                 if (foundInstance != null)
  52.                 {
  53.                     // Select the correct inventory item for interactions
  54.                    
  55.                     if (inputCooldown <= 0f && Input.GetButtonDown("Submit"))
  56.                     {
  57.                         KickStarter.runtimeInventory.SelectItem(foundInstance);
  58.                         interactionMenu.TurnOn();
  59.                         interactionMenu.MatchInteractions(foundInstance, true);
  60.                         Debug.Log("Interactions menu opened - setting item to: " + foundInstance.InvItem.label);
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.  
  66.         private InvInstance GetInventoryInstanceByID(int id)
  67.         {
  68.             if (KickStarter.runtimeInventory == null)
  69.             {
  70.                 Debug.LogError("Runtime Inventory not initialized.");
  71.                 return null;
  72.             }
  73.  
  74.             // Search through player's inventory for an instance of the item
  75.             foreach (InvInstance instance in KickStarter.runtimeInventory.PlayerInvCollection.InvInstances)
  76.             {
  77.                 if (instance.InvItem.id == id)
  78.                 {
  79.                     return instance; // Return the instance, not the InvItem
  80.                 }
  81.             }
  82.  
  83.             return null; // Item not found
  84.         }
  85.  
  86.         private void OnMenuTurnOn(Menu menu, bool isInstant)
  87.         {
  88.             if (menu == inventoryMenu)
  89.             {
  90.                 //KickStarter.runtimeInventory.SetNull();
  91.                 isOn = true;
  92.                 KickStarter.cursorManager.inventoryHandling = InventoryHandling.ChangeCursorAndHotspotLabel;
  93.                 SpawnItems();
  94.  
  95.                 inputCooldown = inputDelay; // Set delay when opening inventory
  96.             }
  97.         }
  98.  
  99.         private void OnMenuTurnOff(Menu menu, bool isInstant)
  100.         {
  101.             if (menu == inventoryMenu)
  102.             {
  103.                 isOn = false;
  104.                 KickStarter.cursorManager.inventoryHandling = InventoryHandling.ChangeCursor;
  105.  
  106.                 foreach (InventoryItemGO spawnedItem in spawnedItems)
  107.                 {
  108.                     Destroy(spawnedItem.gameObject);
  109.                 }
  110.             }
  111.         }
  112.  
  113.         #endregion
  114.  
  115.         #region PrivateFunctions
  116.  
  117.         private void SpawnItems()
  118.         {
  119.             spawnedItems = new InventoryItemGO[KickStarter.runtimeInventory.localItems.Count];
  120.  
  121.             for (int i = 0; i < spawnedItems.Length; i++)
  122.             {
  123.                 if (KickStarter.runtimeInventory.localItems[i].linkedPrefab)
  124.                 {
  125.                     GameObject spawnedItem = Instantiate(KickStarter.runtimeInventory.localItems[i].linkedPrefab, Vector3.zero, Quaternion.identity);
  126.                     spawnedItems[i] = spawnedItem.GetComponent<InventoryItemGO>();
  127.                 }
  128.                 else
  129.                 {
  130.                     GameObject emptyItem = new GameObject();
  131.                     emptyItem.transform.position = Vector3.zero;
  132.                     spawnedItems[i] = emptyItem.AddComponent<InventoryItemGO>();
  133.                 }
  134.             }
  135.         }
  136.  
  137.         #endregion
  138.     }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment