Crafting Menu With Dynamic Category Tabs

desukarhu

Member
Hey,

I'm struggling to figure out how to make a crafting menu with dynamic tabs for categories. The category tabs shown would depend on what crafting categories the Crafter has.

I have a custom crafting menu script with this as my SetCrafter function:
Code:
    public override void SetCrafter (Crafter crafter) {
      m_Crafter = crafter;
      m_Crafter.Initialize (false);
      // Destroy old tabs
      for (int i = 0; i < m_CraftingRecipeGrid.TabControl.m_Content.childCount; i++) {
        Destroy (m_CraftingRecipeGrid.TabControl.m_Content.GetChild (i).gameObject);
      }

      // Create new tabs
      for (int i = 0; i < m_Crafter.CraftingCategories.Length; i++) {
        GameObject tab = Instantiate (CraftingCategoryTab);
        tab.transform.SetParent (m_CraftingRecipeGrid.TabControl.m_Content, false);
        CraftingTabData data = tab.GetComponent<CraftingTabData> ();
        TabToggle toggle = tab.GetComponent<TabToggle> ();
        data.CraftingCategory = m_Crafter.CraftingCategories [i];
        toggle.SetText (m_Crafter.CraftingCategories [i].name);
      }
      m_CraftingRecipeGrid.TabControl.Initialize (true);
      DrawRecipes ();
    }

It works as far as it does instantiate proper tab prefabs, but clicking those tabs does nothing.

Cheers
 
I'm guessing that the tab control doesn't rebind the click events...

Instead of destroying and recreating the tabs. I'd recommend setting the max number of tabs in the editor and then as you loop over them to set the new category, simply disable/enable them as you need them.

Let me know if that works better (it should also be more performant)
 
I'm guessing that the tab control doesn't rebind the click events...

Instead of destroying and recreating the tabs. I'd recommend setting the max number of tabs in the editor and then as you loop over them to set the new category, simply disable/enable them as you need them.

Let me know if that works better (it should also be more performant)
Ugh, I'll give that a go then. I avoided having to do that because I'm going to have quite a lot of crafting categories (each primary material has their own category, like Copper Bar etc.), so I tried doing this to a save me having to do UI work.

I'll let you know how it works out.
 
Let me clarify, Don't make the tab toggle for each possible category, Simply dupplicate the basic Tab Toggle as many times as you'll have the maximum amount of tabs that will be active at any time.
You can assign the category directly in code like you are doing now
 
Let me clarify, Don't make the tab toggle for each possible category, Simply dupplicate the basic Tab Toggle as many times as you'll have the maximum amount of tabs that will be active at any time.
You can assign the category directly in code like you are doing now
Ahh, thank you I got it! I added a bunch of Tab Toggle prefabs and deactivated them and change my code to this:

Code:
      // Hide all tabs
      for (int i = 0; i < m_CraftingRecipeGrid.TabControl.m_Content.childCount; i++) {
        m_CraftingRecipeGrid.TabControl.m_Content.GetChild (i).gameObject.SetActive (false);
      }

      // Show relevant tabs
      for (int i = 0; i < m_Crafter.CraftingCategories.Length; i++) {
        GameObject tab = m_CraftingRecipeGrid.TabControl.m_Content.GetChild (i).gameObject;
        CraftingTabData data = tab.GetComponent<CraftingTabData> ();
        TabToggle toggle = tab.GetComponent<TabToggle> ();
        data.CraftingCategory = m_Crafter.CraftingCategories [i];
        toggle.SetText (m_Crafter.CraftingCategories [i].name);
        
        tab.SetActive (true);
      }

It works now, thanks. I'm unsure why the initialize method didn't work and this does. But I guess the UI needs to initialize all the things in a specific order or something.
 
Top