Inventory.getItemList C# (CSharp) Method

getItemList() public method

public getItemList ( ) : List
return List
    public List<Item> getItemList()
    {
        List<Item> theList = new List<Item>();
        for (int i = 0; i < SlotContainer.transform.childCount; i++)
        {
            if (SlotContainer.transform.GetChild(i).childCount != 0)
                theList.Add(SlotContainer.transform.GetChild(i).GetChild(0).GetComponent<ItemOnObject>().item);
        }
        return theList;
    }

Usage Example

Example #1
0
    private void RefreshInventoryItems()
    {
        foreach (Transform child in itemSlotContainer)
        {
            if (child == itemSlotTemplate)
            {
                continue;
            }
            Destroy(child.gameObject);
        }
        int   x = 0;
        int   y = 0;
        float itemSlotCellSize = 128f;

        foreach (Item item in inventory.getItemList())
        {
            RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, itemSlotContainer).GetComponent <RectTransform>();
            itemSlotRectTransform.gameObject.SetActive(true);

            itemSlotRectTransform.GetComponent <Button_UI>().ClickFunc = () =>
            {
                inventory.UseItem(item);
            };
            itemSlotRectTransform.GetComponent <Button_UI>().MouseRightClickFunc = () =>
            {
                Item duplicateItem = new Item {
                    itemType = item.itemType, amount = item.amount
                };
                inventory.RemoveItem(item);
                ItemWorld.DropItem(player.GetPosition(), duplicateItem);
            };

            itemSlotRectTransform.anchoredPosition = new Vector2(x * itemSlotCellSize, y * itemSlotCellSize);
            Image image = itemSlotRectTransform.Find("Item_Icon").GetComponent <Image>();
            image.sprite = item.getSprite();
            TextMeshProUGUI uiText = itemSlotRectTransform.Find("ItemAmount-text").GetComponent <TextMeshProUGUI>();

            if (item.amount > 1)
            {
                uiText.SetText(item.amount.ToString());
            }
            else
            {
                uiText.SetText("");
            }
            x++;
            if (x > 5)
            {
                x = 0;
                y++;
            }
        }
    }
All Usage Examples Of Inventory::getItemList