Пример #1
0
    private void Inventory_OnItemListRemoved(object sender, Item itemRemoved)
    {
        InventoryItemSlot inventoryItemSlot = GetItemTypeAlreadyInInventory(itemRemoved.itemType);
        RectTransform     itemSlotRectTransform;

        if (inventoryItemSlot != null)
        {
            inventoryItemSlot.SetAmount(inventoryItemSlot.GetAmount() - itemRemoved.amount);

            itemSlotRectTransform = inventoryItemSlot.GetComponent <RectTransform>();

            Transform amountTransform = itemSlotRectTransform.Find("Amount");

            TextMeshProUGUI uiText = amountTransform.Find("Text").GetComponent <TextMeshProUGUI>();

            if (inventoryItemSlot.GetAmount() == 0)
            {
                Destroy(inventoryItemSlot.gameObject);
            }
            else if (inventoryItemSlot.GetAmount() > 1)
            {
                uiText.SetText(inventoryItemSlot.GetAmount().ToString());
                amountTransform.gameObject.SetActive(true);
            }
            else
            {
                amountTransform.gameObject.SetActive(false);
            }
        }
    }
Пример #2
0
    private void Inventory_OnItemListAdded(object sender, Item itemAdded)
    {
        InventoryItemSlot inventoryItemSlot = GetItemTypeAlreadyInInventory(itemAdded.itemType);
        RectTransform     itemSlotRectTransform;

        if (inventoryItemSlot != null)
        {
            itemSlotRectTransform = inventoryItemSlot.GetComponent <RectTransform>();

            Transform amountTransform = itemSlotRectTransform.Find("Amount");

            TextMeshProUGUI uiText = amountTransform.Find("Text").GetComponent <TextMeshProUGUI>();

            inventoryItemSlot.SetAmount(inventoryItemSlot.GetAmount() + itemAdded.amount);

            uiText.SetText(inventoryItemSlot.GetAmount().ToString());
            if (inventoryItemSlot.GetAmount() > 1)
            {
                amountTransform.gameObject.SetActive(true);
            }
            else
            {
                amountTransform.gameObject.SetActive(false);
            }
        }
        else
        {
            itemSlotRectTransform = Instantiate(_itemSlotTemplate, _itemSlotContainer).GetComponent <RectTransform>();
            itemSlotRectTransform.gameObject.SetActive(true);

            itemSlotRectTransform.GetComponent <Button>().onClick.AddListener(() => {
                // Use item
                _inventory.UseItem(itemAdded);
            });

            itemSlotRectTransform.GetComponent <InventoryItemSlot>().SetItemType(itemAdded.itemType);
            itemSlotRectTransform.GetComponent <InventoryItemSlot>().SetAmount(itemAdded.amount);

            Image imagem = itemSlotRectTransform.Find("Image").GetComponent <Image>();
            imagem.sprite = itemAdded.GetInventorySprite();

            Transform amountTransform = itemSlotRectTransform.Find("Amount");
            if (itemAdded.amount > 1)
            {
                TextMeshProUGUI uiText = amountTransform.Find("Text").GetComponent <TextMeshProUGUI>();
                uiText.SetText(itemAdded.amount.ToString());
            }
            else
            {
                amountTransform.gameObject.SetActive(false);
            }
        }
    }