Пример #1
0
 //Change the current selected tab
 public void ChangeCraftingTab(uGUICraftingTab tab)
 {
     //If the tab is already selected just return
     if (selectedTab == tab)
     {
         return;
     }
     //Set the color of all the tabs to inactive
     for (int i = 0; i < craftingTabs.Count; i++)
     {
         craftingTabs[i].GetComponent <Image>().color = tabInactiveColor;
     }
     selectedTab = tab;
     selectedTab.GetComponent <Image>().color = tabActiveColor;
     //Remove all the children of the tabs (the buttons)
     while (scrollContent.transform.childCount != 0)
     {
         DestroyImmediate(scrollContent.transform.GetChild(0).gameObject);
     }
     //Recreate the buttons
     for (int i = 0; i < selectedTab.items.Count; i++)
     {
         uGUICraftingButton item = Instantiate(craftingItemPrefab) as uGUICraftingButton;
         item.transform.SetParent(scrollContent.transform);
         item.transform.localScale = Vector3.one;
         item.item = database.FindCraftItem(int.Parse(selectedTab.items[i].ID));
     }
     //Set the selected crafting item to the first instance
     if (scrollContent.transform.GetChild(0).GetComponent <uGUICraftingButton>())
     {
         ChangeCraftingItem(scrollContent.transform.GetChild(0).GetComponent <uGUICraftingButton>());
     }
     //Reset the scrolling position
     scrollRect.verticalNormalizedPosition = 1f;
 }
Пример #2
0
    // Use this for initialization
    void Start()
    {
        //Find the player
        player = GameObject.FindGameObjectWithTag("Player").GetComponent <Player>();
        //Find the inventory
        inventory = GameObject.FindGameObjectWithTag("Inventory").GetComponent <uGUIInventory>();

        craftingTabs = new List <uGUICraftingTab>();
        selectedItem = new CraftedItem();

        //Run through all the craft item IDs
        for (int i = 0; i < craftAbleItemIDs.Count; i++)
        {
            //Find the item in the item database
            CraftedItem item = database.FindCraftItem(craftAbleItemIDs[i]);
            //If there is already any crafting tabs
            if (craftingTabs.Count > 0)
            {
                int counter = 0;
                //Run through all the crafting tabs
                for (int j = 0; j < craftingTabs.Count; j++)
                {
                    //If the item base type is = to the tabtype of the crafting tab then add the item to the list
                    if (craftingTabs[j].tabType == item.baseType)
                    {
                        if (craftingTabs[j].items == null)
                        {
                            craftingTabs[j].items = new List <CraftedItem>();
                        }
                        craftingTabs[j].items.Add(item);
                        counter++;
                    }
                    //If we're at the end of the list and there's no tab with the current item type already
                    //Instantiate the tab and add the item to the list of items in the tab
                    if (j == craftingTabs.Count - 1 && counter == 0)
                    {
                        GameObject      tempTab = Instantiate(tabPrefab) as GameObject;
                        uGUICraftingTab tab     = tempTab.AddComponent <uGUICraftingTab>();
                        tab.transform.SetParent(tabsObj.transform);
                        tab.GetComponent <RectTransform>().localScale = Vector3.one;
                        tab.tabType = item.baseType;
                        if (tab.tabType == CraftingTabType.armor)
                        {
                            tab.GetComponent <Image>().sprite = armorTab;
                        }
                        else if (tab.tabType == CraftingTabType.weapon)
                        {
                            tab.GetComponent <Image>().sprite = weaponTab;
                        }
                        craftingTabs.Add(tab);
                        tabsObj.GetComponent <RectTransform>().sizeDelta = new Vector3(tabWidth, tabHeight * craftingTabs.Count);
                    }
                }
            }
            //There isn't any tabs
            //Instantiate the tab and add the item
            else
            {
                GameObject      tempTab = Instantiate(tabPrefab) as GameObject;
                uGUICraftingTab tab     = tempTab.AddComponent <uGUICraftingTab>();
                tab.transform.SetParent(tabsObj.transform);
                tab.GetComponent <RectTransform>().localScale = Vector3.one;
                tab.tabType = item.baseType;
                if (tab.tabType == CraftingTabType.armor)
                {
                    tab.GetComponent <Image>().sprite = armorTab;
                }
                else if (tab.tabType == CraftingTabType.weapon)
                {
                    tab.GetComponent <Image>().sprite = weaponTab;
                }
                craftingTabs.Add(tab);
                craftingTabs[craftingTabs.IndexOf(tab)].items = new List <CraftedItem>();
                craftingTabs[craftingTabs.IndexOf(tab)].items.Add(item);
                tabsObj.GetComponent <RectTransform>().sizeDelta =
                    new Vector3(tabWidth, tabHeight * craftingTabs.Count);
            }
        }
        //Set the current selected tab to the first instance of the tabs
        selectedTab = craftingTabs[0];

        //Set the color of all the tabs to inactive
        for (int i = 0; i < craftingTabs.Count; i++)
        {
            craftingTabs[i].GetComponent <Image>().color = tabInactiveColor;
        }
        //Set the selected tabs color to active
        selectedTab.GetComponent <Image>().color = tabActiveColor;

        //Run through all the items in the selected tab and instantiate them
        for (int i = 0; i < selectedTab.items.Count; i++)
        {
            uGUICraftingButton item = Instantiate(craftingItemPrefab) as uGUICraftingButton;
            item.transform.SetParent(scrollContent.transform);
            item.transform.localScale   = Vector3.one;
            selectedTab.items[i].button = item;
            if (!string.IsNullOrEmpty(selectedTab.items[i].ID))
            {
                item.item = database.FindCraftItem(int.Parse(selectedTab.items[i].ID));
            }

            item.transform.name = i.ToString();
        }

        //Close the crafting window
        OpenCloseWindow(false);
    }
Пример #3
0
    //Change the selected crafting item
    public void ChangeCraftingItem(uGUICraftingButton item)
    {
        //Position the selected highlight on top of the selected item
        buttonHighlight.transform.position = item.transform.position;

        selectedItem = item.item;

        //Instantiate all the materials
        materialAmounts = new List <int>();
        for (int i = 0; i < item.item.materials.Count; i++)
        {
            if (materials.Count == 0)
            {
                for (int j = 0; j < item.item.materials.Count; j++)
                {
                    uGUICraftingMaterial material = Instantiate(materialPrefab) as uGUICraftingMaterial;
                    materials.Add(material);
                }
            }
        }
        //Position all the materials
        for (int j = 0; j < materials.Count; j++)
        {
            bool avaliable = false;
            materials[j].transform.SetParent(materialsListGameObject.transform);
            materials[j].transform.localScale = Vector3.one;
            materials[j].icon.sprite          = item.item.materials[j].icon;
            materials[j].item = item.item.materials[j];
            int count = 0;
            for (int k = 0; k < inventory.items.Count; k++)
            {
                if (inventory.items[k].item.itemName == item.item.materials[j].itemName)
                {
                    count += inventory.items[inventory.items[k].itemStartNumber].item.stackSize;
                }
            }
            if (count < item.item.materialRequiredAmount[j])
            {
                materials[j].text.color = Color.red;
                Stop();
                avaliable = false;
            }
            else
            {
                materials[j].text.color   = Color.white;
                acceptButton.interactable = true;
            }
            if (!avaliable)
            {
                acceptButton.interactable = false;
            }
            materials[j].text.text = count.ToString() + "/" + item.item.materialRequiredAmount[j];
            materialAmounts.Add(count / item.item.materialRequiredAmount[j]);
        }
        craftingItemImage.sprite = item.item.item.icon;
        craftingItemImage.rectTransform.sizeDelta =
            new Vector2(item.item.item.width *
                        iconSlotSize, item.item.item.height * iconSlotSize);
        selectedItemNameText.text = item.item.item.itemName;
        craftCostLabel.text       = (selectedItem.craftCost * amountToCraft).ToString();
        craftCostLabel.rectTransform.sizeDelta = new Vector2(craftCostLabel.preferredWidth, craftCostLabel.rectTransform.sizeDelta.y);
    }