dronkowitz

InventoryUI.cs

Nov 4th, 2022 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class InventoryUI : MonoBehaviour
  6. {
  7.     public GameObject inventoryScreen;
  8.     public Inventory connectedInventory;
  9.     public Transform content;
  10.     public InventorySlot[] slots;
  11.     public InventorySlot slotPrefab;
  12.     public Item heldItem;
  13.     public Image heldItemImage;
  14.  
  15.     private void Start()
  16.     {
  17.         SetupInventoryUI();
  18.     }
  19.     public void SetupInventoryUI()
  20.     {
  21.         connectedInventory.inventoryUpdated += UpdateInventoryScreen;
  22.         if (slots.Length == 0)
  23.         {
  24.             slots = new InventorySlot[connectedInventory.backPack.Length];
  25.             for (int i = 0; i < connectedInventory.backPack.Length; i++)
  26.             {
  27.                 InventorySlot newSlot = Instantiate(slotPrefab, content);
  28.                 if (connectedInventory.backPack[i] != null)
  29.                 {
  30.                     newSlot.itemImage.sprite = connectedInventory.backPack[i].itemImage;
  31.                    
  32.                 }
  33.                 newSlot.slotClicked += SetHeldItem;
  34.                 slots[i] = newSlot;
  35.             }
  36.         }
  37.     }
  38.     public void UpdateInventoryScreen()
  39.     {
  40.         for (int i = 0; i < slots.Length; i++)
  41.         {
  42.            
  43.             if (connectedInventory.backPack[i] != null)
  44.             {
  45.                 slots[i].itemImage.sprite = connectedInventory.backPack[i].itemImage;
  46.             }
  47.             else
  48.             {
  49.                 slots[i].itemImage.sprite = null;
  50.             }
  51.         }
  52.  
  53.         if (heldItem != null)
  54.         {
  55.             heldItemImage.enabled = true;
  56.             heldItemImage.sprite = heldItem.itemImage;
  57.         }
  58.         else
  59.         {
  60.             heldItemImage.enabled = false;
  61.         }
  62.     }
  63.  
  64.     public void SetHeldItem(InventorySlot selectedSlot)
  65.     {
  66.         int index = System.Array.IndexOf(slots, selectedSlot);
  67.         Debug.Log("Clicking slot " + index);
  68.         Item previousHeldItem = heldItem;
  69.         heldItem = connectedInventory.backPack[index];
  70.         connectedInventory.backPack[index] = previousHeldItem;
  71.  
  72.        
  73.  
  74.         UpdateInventoryScreen();
  75.     }
  76.  
  77.     public void ShowInventory()
  78.     {
  79.         inventoryScreen.SetActive(true);
  80.         UpdateInventoryScreen();
  81.     }
  82.    
  83.     public void HideInventory()
  84.     {
  85.         inventoryScreen?.SetActive(false);
  86.         if (heldItem != null)
  87.         {
  88.             connectedInventory.AddToBackPack(heldItem);
  89.             heldItem = null;
  90.         }
  91.         UpdateInventoryScreen();
  92.     }
  93.  
  94.     private void Update()
  95.     {
  96.         if (Input.GetKeyDown(KeyCode.I))
  97.         {
  98.             if (inventoryScreen.activeSelf)
  99.             {
  100.                 HideInventory();
  101.             }
  102.  
  103.             else
  104.             {
  105.                 ShowInventory();
  106.             }
  107.         }
  108.     }
  109. }
  110.  
Add Comment
Please, Sign In to add comment