/// <summary> /// Creates UI item, plays its "get" animation and adds it to list of active UI items /// </summary> /// <param name="key">Item to create a UI item from</param> public void AddItem(InteractableItemKey key) { // If slot already exists, make that item fill the slot foreach (InventoryItemUI slotItem in slotItems) { if (slotItem.itemKey == key) { slotItem.PlayItemGetAnimation(); activeItems.Add(slotItem); slotItems.Remove(slotItem); return; } } // else create new item and immediately fill the slot GameObject item = Instantiate(itemPrefab, itemParent); InventoryItemUI itemUI = item.GetComponent <InventoryItemUI>(); if (itemUI == null) { Debug.LogError("Item prefab in InventoryUI does not have a InventoryItemUI component"); Destroy(item); } else { itemUI.Init(key); itemUI.PlayItemGetAnimation(); activeItems.Add(itemUI); } }
/// <summary> /// Adds an empty item slot to the UI /// </summary> /// <param name="key">Key to show empty slot for</param> public void AddItemSlot(InteractableItemKey key) { GameObject item = Instantiate(itemPrefab, itemParent); InventoryItemUI itemUI = item.GetComponent <InventoryItemUI>(); if (itemUI == null) { Debug.LogError("Item prefab in InventoryUI does not have a InventoryItemUI component"); Destroy(item); } else { itemUI.Init(key); slotItems.Add(itemUI); } }
private void OnEnable() { PlayerMyController.Instance.EnabledWindowCount++; int capacity = PlayerMyController.Instance.InventoryCapacity; int count = PlayerMyController.Instance.Inventory.Count; foreach (var kv in PlayerMyController.Instance.Inventory) { GameObject cloned = GameObject.Instantiate(InventoryCell); Button button = cloned.GetComponent <Button>(); // TODO ... specify icon by item types Sprite icon = GetAllIcons.icons["Sword_2"]; button.image.sprite = icon; cloned.SetActive(true); cloned.transform.SetParent(InventoryGridContent.transform, false); InventoryItemUI handler = cloned.GetComponent <InventoryItemUI>(); handler.Init("Sword_2"); } foreach (var kv in TreasureInfo.playerTreasure) { if (kv.Value.wear == true & kv.Value.number == 1) { continue; } Debug.Log("inventory: " + kv.Key); GameObject cloned = GameObject.Instantiate(InventoryCell); Button button = cloned.GetComponent <Button>(); Sprite icon = GetAllIcons.icons[kv.Key]; button.image.sprite = icon; cloned.SetActive(true); cloned.transform.SetParent(InventoryGridContent.transform, false); InventoryItemUI handler = cloned.GetComponent <InventoryItemUI>(); handler.Init(kv.Key); } count += TreasureInfo.playerTreasure.Count; // genreate empty cell for (int i = 0; i < capacity - count; i++) { GameObject cloned = GameObject.Instantiate(InventoryCell); cloned.SetActive(true); cloned.transform.SetParent(InventoryGridContent.transform, false); } }