private void RefreshInventoryItems() { DestroyItemContainerChildTransforms(); for (int itemIndex = 0; itemIndex < _playerInventory.GetLenght(); itemIndex++) { RectTransform itemSlotRectTransform = Instantiate(_itemSlotTemplate, _itemContainer).GetComponent <RectTransform>(); Image itemIcon = itemSlotRectTransform.Find("ItemImage").GetComponent <Image>(); TextMeshProUGUI itemCount = itemSlotRectTransform.Find("ItemCount").GetComponent <TextMeshProUGUI>(); TextMeshProUGUI itemCooldown = itemSlotRectTransform.Find("ItemCooldown").GetComponent <TextMeshProUGUI>(); InventoryMouseInteraction inventoryMouseInteraction = itemSlotRectTransform.GetComponent <InventoryMouseInteraction>(); CreateItemButton(itemIndex, itemSlotRectTransform, itemIcon, itemCount, itemCooldown, inventoryMouseInteraction); } }
/// <summary> /// Creates an item button /// </summary> /// <param name="itemIndex">item index</param> /// <param name="itemSlotRectTransform"> item slot rect transform</param> /// <param name="itemIcon">item icon image</param> /// <param name="itemCount">item count TMP</param> /// <param name="itemCooldown">item cooldown TMP</param> /// <param name="inventoryMouseInteraction">mouse interaction</param> private void CreateItemButton(int itemIndex, RectTransform itemSlotRectTransform, Image itemIcon, TextMeshProUGUI itemCount, TextMeshProUGUI itemCooldown, InventoryMouseInteraction inventoryMouseInteraction) { inventoryMouseInteraction.ItemIndex = itemIndex; inventoryMouseInteraction.onDropEvent.AddListener(OnDrop); itemSlotRectTransform.gameObject.SetActive(true); if (_playerInventory.GetItem(itemIndex) != null) { itemIcon.sprite = CreateSprite(_playerInventory.GetIcon(itemIndex)); inventoryMouseInteraction.IsDragable = true; string itemName = _playerInventory.GetName(itemIndex); if (_typesOnCooldown.ContainsKey(itemName)) { SetItemCooldown(_typesOnCooldown[itemName], itemIcon, itemCooldown, itemCount); } else { SetItemNormal(_playerInventory.GetItemCount(itemIndex), itemIcon, itemCooldown, itemCount); Type type = _playerInventory.GetItemType(itemIndex); if (type == typeof(Consumable)) { inventoryMouseInteraction.doubleClickEvent.AddListener(UseConsumable); } else if (type == typeof(Weapon) || type == typeof(Armor)) { inventoryMouseInteraction.doubleClickEvent.AddListener(EquipItem); } } } else { inventoryMouseInteraction.IsDragable = false; } }