SHARE
TWEET

Untitled

a guest Aug 23rd, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class Inventory : MonoBehaviour
  6. {
  7.     public GameObject slotPrefab;
  8.     public const int numSlots = 5;
  9.     Image[] itemImages = new Image[numSlots];
  10.     Item[]items = new Item[numSlots];
  11.     GameObject[] slots = new
  12.         GameObject[numSlots];
  13.  
  14.     public void CreateSlots()
  15.     {
  16.         if(slotPrefab != null)
  17.         {
  18.             for(int i =0; i < numSlots; i++)
  19.             {
  20.                 GameObject newSlot = Instantiate(slotPrefab);
  21.                 newSlot.name = "ItemSlot_" + i;
  22.                 newSlot.transform.SetParent(gameObject.transform.GetChild(0).transform);
  23.                 slots[i] = newSlot;
  24.                 itemImages[i] = newSlot.transform.GetChild(1).GetComponent<Image>();
  25.             }
  26.         }
  27.     }
  28.     public bool AddItem(Item itemToAdd)
  29.     {
  30.         for (int i = 0; i < items.Length; i++)
  31.         {
  32.             if(items[i] != null && items[i].itemType == itemToAdd.itemType && itemToAdd.stackable == true)
  33.             {
  34.                 items[i].quantity = items[i].quantity + 1;
  35.                 Slot slotScript = slots[i].gameObject.GetComponent<Slot>();
  36.                 Text quantityText = slotScript.qtyText;
  37.                 quantityText.enabled = true;
  38.                 quantityText.text = items[i].quantity.ToString();
  39.                 return true;
  40.             }
  41.             if (items[i] == null)
  42.             {
  43.                 items[i] = Instantiate(itemToAdd);
  44.                 items[i].quantity = 1;
  45.                 itemImages[i].sprite = itemToAdd.sprite;
  46.                 itemImages[i].enabled = true;
  47.                 return true;
  48.  
  49.             }
  50.         }
  51.         return false;
  52.     }
  53.  
  54.     public void Start()
  55.     {
  56.         CreateSlots();
  57.     }
  58.  
  59.  
  60.     void Update()
  61.     {
  62.        
  63.     }
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top