Advertisement
Guest User

InventoryObject

a guest
Apr 6th, 2021
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [CreateAssetMenu(fileName = "New Inventory", menuName = "Inventory System / Inventory")]
  5. public class InventoryObject : ScriptableObject
  6. {
  7.  
  8.     [SerializeField] public List<InventorySlot> Container = new List<InventorySlot>(4);
  9.     public void AddItem(ItemObject _item, int _amount)
  10.     {
  11.         if (Container.Count > 0)
  12.         {
  13.             for (int i = 0; i < Container.Count; i++)
  14.             {
  15.                 if (Container[i].itemContained.itemName == _item.itemName)
  16.                 {
  17.                     if (Container[i].itemContained.stackable == false)
  18.                     {
  19.                         Container.Add(new InventorySlot(_item, _amount));
  20.                         break;
  21.                     }
  22.                     else
  23.                     {
  24.                         Container[i].amount += _amount;
  25.                         break;
  26.                     }
  27.  
  28.                 }
  29.                 else
  30.                 {
  31.                     Container.Add(new InventorySlot(_item, _amount));
  32.  
  33.                 }
  34.             }
  35.         }
  36.         else
  37.         {
  38.             Container.Add(new InventorySlot(_item, _amount));
  39.         }
  40.      
  41.  
  42.     }
  43.  
  44.     void RemoveAmount(ItemObject _item, int _amount)
  45.     {
  46.         for (int i = 0; i < Container.Count; i++)
  47.         {
  48.             if (Container[i].itemContained == _item)
  49.             {
  50.                 Container[i].amount -= _amount;
  51.             }
  52.         }
  53.     }
  54.  
  55.     public class InventorySlot
  56.     {
  57.         public int amount;
  58.         public ItemObject itemContained;
  59.         public InventorySlot(ItemObject _item, int _amount)
  60.         {
  61.             amount = _amount;
  62.             itemContained = _item;
  63.         }
  64.         public void AddAmount(int _amount)
  65.         {
  66.             amount = _amount;
  67.         }
  68.  
  69.  
  70.     }
  71.    
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement