//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; }
//Add an item to the tabs public void AddCraftingItem(int ID) { CraftedItem item = database.FindCraftItem(ID); if (craftingTabs.Count > 0) { int counter = 0; for (int j = 0; j < craftingTabs.Count; j++) { if (craftingTabs[j].tabType == item.baseType) { if (craftingTabs[j].items == null) { craftingTabs[j].items = new List <CraftedItem>(); } craftingTabs[j].items.Add(item); counter++; } 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); } } } }
// 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); }