/**<summary> Equip an ItemData item </summary>*/ private void EquipItem(ItemData itemData) { GameObject obj = (GameObject)Resources.Load(itemData.itemPrefab); if (!obj) { obj = Resources.Load <GameObject>("Item"); } Item item = (Instantiate(obj)).GetComponent <Item>(); itemData.gObject = item.gameObject; item.data = itemData; equippedItems.Add(item); // Unequip oldest item in case max limit reaceh if (equippedItems.Count > equipItemLimit) { UnequipItem(0); } else { item.gameObject.SetActive(false); } // Select as active if only one item equipped if (data.equippedItems.Count == 1) { SelectItemAsActive(0); } RefreshInventory(); OnEquippedItemsChanged?.Invoke(equippedItems.Select(i => i.data).ToArray()); }
/**<summary> Unequip an Item </summary>*/ private void UnequipItem(int itemIndex) { if (equippedItems.Count > 0) { Destroy(equippedItems[itemIndex].gameObject); equippedItems.RemoveAt(itemIndex); SelectItemAsActive(equippedItems.Count - 1); } if (equippedItems.Count == 0) { data.activeItemIndex = -1; OnActiveItemChanged?.Invoke(new ItemData(), -1); } RefreshInventory(); OnEquippedItemsChanged?.Invoke(equippedItems.Select(i => i.data).ToArray()); }