Advertisement
Diamond32_Tutoriales

SYSTEMA DE INVENTARIO

Nov 26th, 2020
1,064
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. ///////MANAGER
  2.  
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7.  
  8. public class InventoryManager : MonoBehaviour
  9. {
  10.     public GameObject SlotManager;
  11.     public GameObject[] Slots;
  12.     int allSlots;
  13.  
  14.      void Start()
  15.     {
  16.         allSlots = SlotManager.transform.childCount;
  17.         Slots = new GameObject[allSlots];
  18.     for (int i = 0; i < allSlots; i++)
  19.         {
  20.             Slots[i] = SlotManager.transform.GetChild(i).gameObject;
  21.  
  22.             Slots[i].gameObject.GetComponent<Slot>().Empty = true;
  23.         }
  24.     }
  25.  
  26.      void Update()
  27.     {
  28.        
  29.     }
  30.  
  31.  
  32.     public void PickUpItem(GameObject item)
  33.     {
  34.         Debug.Log("Item Recogido");
  35.         for (int i = 0; i < allSlots; i++)
  36.         {
  37.             if (Slots[i].GetComponent<Slot>().Empty)
  38.             {
  39.                
  40.                 item.GetComponent<Item>().Pickup = true;
  41.                 Slots[i].GetComponent<Slot>().Empty = false;
  42.                 Slots[i].GetComponent<Slot>().objecto = item;
  43.                 Slots[i].GetComponent<Image>().sprite = item.GetComponent<Item>().ImageType;
  44.                 item.transform.parent = Slots[i].transform;
  45.                 return;
  46.             }
  47.         }
  48.     }
  49.  
  50.     private void OnTriggerEnter(Collider cl)
  51.     {
  52.         if (cl.tag == "Item")
  53.         {
  54.             GameObject item = cl.gameObject;
  55.             PickUpItem(item);
  56.         }
  57.     }
  58. }
  59.  
  60.  
  61.  
  62.  
  63. //////////////ITEMS
  64.  
  65. using System.Collections;
  66. using System.Collections.Generic;
  67. using UnityEngine;
  68.  
  69. public enum Items {
  70.     Manzana,
  71.     Hacha,
  72.     Pan,
  73.     Pico,
  74.     Pistola
  75. };
  76.  
  77. public class Item : MonoBehaviour
  78. {
  79.     public Items tipo;
  80.     public Sprite ImageType;
  81.     public bool Pickup;
  82.  
  83.      void Start()
  84.     {
  85.        
  86.     }
  87.  
  88.      void Update()
  89.     {
  90.        
  91.     }
  92.  
  93.  
  94.     void Use()
  95.     {
  96.     switch (tipo)
  97.         {
  98.             case Items.Hacha:
  99.                 {
  100.                     Debug.Log("Hacha");
  101.                     Destroy(this.gameObject);
  102.                     break;
  103.                 }
  104.             case Items.Manzana:
  105.                 {
  106.                     Debug.Log("Manzana");
  107.                     Destroy(this.gameObject);
  108.                     break;
  109.                 }
  110.             case Items.Pan:
  111.                 {
  112.                     Debug.Log("Pan");
  113.                     Destroy(this.gameObject);
  114.                     break;
  115.                 }
  116.  
  117.             case Items.Pico:
  118.                 {
  119.                     Debug.Log("Pico");
  120.                     Destroy(this.gameObject);
  121.                     break;
  122.                 }
  123.  
  124.             case Items.Pistola:
  125.                 {
  126.                     Debug.Log("Pistola");
  127.                     Destroy(this.gameObject);
  128.                     break;
  129.                 }
  130.         }
  131.     }
  132.  
  133.     public void OnClickObject()
  134.     {
  135.          Use();
  136.     }
  137. }
  138.  
  139. ///SLOTS
  140.  
  141.  
  142. using System.Collections;
  143. using System.Collections.Generic;
  144. using UnityEngine;
  145. using UnityEngine.EventSystems;
  146. using UnityEngine.UI;
  147.  
  148. public class Slot : MonoBehaviour, IPointerClickHandler
  149. {
  150.  
  151.     public bool Empty;
  152.     public GameObject objecto;
  153.  
  154.      void Update()
  155.     {
  156.         if (objecto == null)
  157.         {
  158.             Empty = true;
  159.             GetComponent<Image>().sprite = null;
  160.         }
  161.         else
  162.         {
  163.             Empty = false;
  164.         }
  165.     }
  166.  
  167.     public void OnPointerClick(PointerEventData pointerEventData)
  168.     {
  169.         Debug.Log("Slots Funcionando");
  170.         objecto.GetComponent<Item>().OnClickObject();
  171.     }
  172. }
  173.  
  174.  
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement