public void SetInventory(Inventory inventory) { // Removing old buttons. foreach (ItemButtonUI invItemButton in invItemButtons) { Destroy(invItemButton.gameObject); } invItemButtons.Clear(); InventoryItem[] items = inventory.GetItems(); for (int i = 0; i < items.Length; i++) { ItemButtonUI itemBtn = GetInvItemButtonPrefab(items[i].itemType); ItemButtonUI itemBtnInstance = Instantiate(itemBtn, inventoryPanel.transform); itemBtnInstance.SetItemCount(items[i].count); invItemButtons.Add(itemBtnInstance); } // Setup Remove Item Button. ItemButtonUI removeItemBtn = GetInvItemButtonPrefab(ItemType.None); ItemButtonUI removeItemBtnInstance = Instantiate(removeItemBtn, inventoryPanel.transform); invItemButtons.Add(removeItemBtnInstance); // Setup Button Press Listeners. for (int i = 0; i < invItemButtons.Count; i++) { int index = i; invItemButtons[i].GetComponent <Button>().onClick.AddListener(() => InvItemButtonPressed(index)); } // Set default selected button to active. invItemButtons[selectedInvItemButtonIdx].SetActiveState(true); }
public void SetWeaponsSlot(Weapon[] weapons, int activeWeapon) { foreach (Transform child in this.weapons.transform) { Destroy(child.gameObject); } float itemSlotSize = ItemSlotSample.GetComponent <RectTransform>().rect.width; for (int i = 0; i < weapons.Length; i++) { RectTransform weaponSlotRect = Instantiate(ItemSlotSample, this.weapons.transform).GetComponent <RectTransform>(); weaponSlotRect.gameObject.SetActive(true); weaponSlotRect.anchoredPosition = new Vector2(((itemSlotSize / 2) + itemSlotSize) * i + itemSlotSize, ((itemSlotSize / 2) + itemSlotSize)); ItemButtonUI butt = weaponSlotRect.gameObject.GetComponent <ItemButtonUI>(); if (weapons[i] != null) { weaponSlotRect.gameObject.GetComponent <Image>().sprite = Instantiate(weapons[i].icon); butt.onRightMouseClick += onWeaponRightMouseClick; } } }
public void SetOnRightClick(EventHandler <PointerEventData> eventHandler) { foreach (Transform item in bag.transform) { ItemButtonUI butt = item.gameObject.GetComponent <ItemButtonUI>(); butt.onRightMouseClick -= onRightMouseClick; butt.onRightMouseClick += eventHandler; } }
public void SetCraftItems() { foreach (Transform item in itemsToCraft.transform) { Destroy(item.gameObject); } RectTransform itemSlotTransform = ItemSlot.GetComponent <RectTransform>(); RectTransform itemsToCraftTransform = itemsToCraft.GetComponent <RectTransform>(); float slotWidth = itemSlotTransform.rect.width; float slotHeight = itemSlotTransform.rect.height; Debug.Log("ItemstoCraft Width: " + itemsToCraftTransform.rect.width); Debug.Log("itemtocraft Count: " + craftItems.Count); itemsToCraftTransform.sizeDelta = new Vector2(itemsToCraftTransform.rect.width, craftItems.Count * (slotHeight + slotHeight / 2)); itemsToCraftTransform.anchoredPosition = new Vector2(0, -craftItems.Count * (slotHeight / 2)); for (int i = 0; i < craftItems.Count; i++) { RectTransform newItemSlot = Instantiate(ItemSlot, itemsToCraft.transform).GetComponent <RectTransform>(); newItemSlot.anchoredPosition = new Vector2(slotWidth / 2 + slotWidth, i * (slotHeight + slotHeight / 2) + slotHeight / 2); ItemContainer itemContainer = newItemSlot.gameObject.GetComponent <ItemContainer>(); itemContainer.item = craftItems[i]; newItemSlot.gameObject.SetActive(true); ItemButtonUI butt = newItemSlot.gameObject.GetComponent <ItemButtonUI>(); if (butt == null) { Debug.Log("No ItemButtonUI in ItemSlot Prefab: CraftUI, SetCraftItems"); break; } Item curItem = craftItems[i]; Image icon = newItemSlot.gameObject.GetComponent <Image>(); icon.sprite = curItem.icon; butt.onRightMouseClick += onRightMouseClick; } }
public void SetItems(List <Item> items) { foreach (Transform child in bag.transform) { Destroy(child.gameObject); } RectTransform bagRectTransform = bag.GetComponent <RectTransform>(); bagRectTransform.sizeDelta = new Vector2(bagRectTransform.rect.width, items.Count * 75); bagRectTransform.anchoredPosition = new Vector2(0, -items.Count * 25); float itemSlotSize = ItemSlotSample.GetComponent <RectTransform>().rect.width; for (int i = 0; i < items.Count; i++) { RectTransform ItemSlotRect = Instantiate(ItemSlotSample, bag.transform).GetComponent <RectTransform>(); ItemSlotRect.gameObject.SetActive(true); ItemSlotRect.anchoredPosition = new Vector2((itemSlotSize / 2) + itemSlotSize, (((itemSlotSize / 2) + itemSlotSize) * i) + itemSlotSize / 2); Item curItem = inventory.GetItem(i); if (curItem != null) { ItemContainer itemContainer = ItemSlotRect.gameObject.GetComponent <ItemContainer>(); itemContainer.item = curItem; ItemButtonUI butt = ItemSlotRect.gameObject.GetComponent <ItemButtonUI>(); butt.onLeftMouseClick += onLeftMouseClick; butt.onRightMouseClick += onRightMouseClick; butt.onDoubleLeftMouseClick += onDoubleMouseClick; Image icon = ItemSlotRect.gameObject.GetComponent <Image>(); if (curItem.stackCount > 1) { Text stackCount = ItemSlotRect.gameObject.transform.Find("StackCount").gameObject.GetComponent <Text>(); stackCount.text = curItem.stackCount.ToString(); } icon.sprite = curItem.icon; } } }
private void BuildLootUI() { foreach (Transform lootItem in loot.transform) { Destroy(lootItem.gameObject); } float itemSlotSize = itemSlotSample.GetComponent <RectTransform>().rect.width; Vector2 windowSize = loot.GetComponent <RectTransform>().rect.size; loot.GetComponent <RectTransform>().sizeDelta = new Vector2(windowSize.x, items.Count * (itemSlotSize + itemSlotSize / 2)); for (int i = 0; i < items.Count; i++) { loot.GetComponent <RectTransform>().anchoredPosition = new Vector2(0, -items.Count * (itemSlotSize / 2)); } for (int i = 0; i < items.Count; i++) { RectTransform ItemSlotRect = Instantiate(itemSlotSample, loot.transform).GetComponent <RectTransform>(); ItemSlotRect.gameObject.SetActive(true); ItemSlotRect.anchoredPosition = new Vector2(((itemSlotSize / 2) + itemSlotSize), windowSize.y - ((itemSlotSize / 2) + itemSlotSize) * i); Item curItem = items[i]; if (curItem != null) { ItemButtonUI butt = ItemSlotRect.gameObject.GetComponent <ItemButtonUI>(); butt.onLeftMouseClick += onLeftMouseClick; Image icon = ItemSlotRect.gameObject.GetComponent <Image>(); icon.sprite = curItem.icon; ItemSlotRect.gameObject.GetComponent <ItemContainer>().item = curItem; } } }