/// <summary> /// Removes an item from the inventory UI or decreases the stack size by one. /// Deletes a item button if the count is 0. /// </summary> /// <param name="item">The item to remove from the UI</param> private void RemoveItem(Item item) { ItemButton itemButtonInList = _items.Find(button => button.Item == item); if (itemButtonInList == default(ItemButton)) { return; } itemButtonInList.Count--; // Skip deletion if amount is left if (itemButtonInList.Count > 0) { return; } RemoveItemButton(itemButtonInList); }
/// <summary> /// Adds an item to the inventory UI or increases the stack size in case it already exists. /// Creates a new item button if the previous ones are full. /// </summary> /// <param name="item">The item to add to the UI</param> private void AddItem(Item item) { ItemButton itemButtonInList = _items.Find(button => button.Item == item && button.CanAddOne()); // Add to first one found if (itemButtonInList != default(ItemButton)) { itemButtonInList.Count++; return; } // Only full stacks or no stack yet ItemButton itemButton = Instantiate(itemButtonPrefab, itemGrid.transform); itemGrid.SetPositionInGrid(itemButton.gameObject); itemButton.Init(item); _items.Add(itemButton); }
/// <summary> /// Removes an ItemButton from the UI and adds an empty space. /// </summary> /// <param name="itemButton"></param> private void RemoveItemButton(ItemButton itemButton) { itemGrid.AddEmptySlotAt(itemButton.transform.GetSiblingIndex()); DestroyImmediate(itemButton.gameObject); _items.Remove(itemButton); }