Create multiple Foldouts

Hi All,

I am trying to learn unity3d with c# but it is hard going! I have created a custom inspector with a simple single button. When the button is clicked It should create a foldout. If the button is clicked again it should display foldout2, then foldout3 etc. I can show you what I have managed to do so far…

using System;
using System.IO;
using UnityEngine;
using UnityEditor;
using System.Text;
using System.Collections;

public class Foldouts : EditorWindow
{
    bool MyFoldout3 = false;
    bool MyFoldout4 = false;
    int increment = 0;

    // Add menu named "Window/your script name" to the Window menu
    [MenuItem("Window/Foldouts/Open")]
    static void Init()
    {
        // Get existing open window or if none, make a new one:
        Foldouts window = (Foldouts)EditorWindow.GetWindow(typeof(Foldouts));
    }

    void OnGUI() // void means won't return a value
    {
        GUILayout.BeginHorizontal();
        GUILayout.Label(" ", EditorStyles.boldLabel);
        if (GUILayout.Button("Make Foldout", GUILayout.Width(100)))
        {
            MyFoldout3 = EditorGUILayout.Foldout(MyFoldout3, "About.");
            if (MyFoldout3)
            {
                GUILayout.Label("automated foldout", EditorStyles.largeLabel);

            }
        }
        GUILayout.EndHorizontal();
        MyFoldout4 = EditorGUILayout.Foldout(MyFoldout4, "hello World");
        if (MyFoldout4)
        {
            EditorGUILayout.Space();
            GUILayout.Label("Just testing", EditorStyles.whiteLargeLabel);


        }
    }
}

being a newby to programming I think I need a counter variable, a create new foldout function as such and something that creates the new foldout name/number. Just not sure how to go about it yet or even where to start.

Thanks

john

You should use a List to store your bools and keep track of the ammount of “created” Foldouts, you will extend this later, but this should do it at first:

Add a new using:

using System.Collections.Generic;

Create a list to store the foldouts parameters:

private List<bool> myFooldOuts = new List<bool>();

Create a new entry to this list when the button has been pressed:

if (GUILayout.Button("Make Foldout", GUILayout.Width(100)))
{
    myFoldOuts.Add(false);
}

And then display the foldouts:

for (int i = 0; i < myFoldOuts.Count; ++i)
{
    var foldoutName = string.Format("{0}. Foldout", i + 1);
    myFoldOuts[i] = EditorGUILayout.Foldout(myFoldOuts[i], foldoutName);
    if (myFoldOuts[i])
    {
        EditorGUILayout.Space();

        GUILayout.Label("Just testing", EditorStyles.whiteLargeLabel);
    }
}

Extending this should be very easy, so have fun with it :slight_smile:

Thanks for that mark, that was really helpful. I would never have got any of that. lol

Just a note. Love the suggested code. But when I cut and pasted, it didn’t work at first. Turned out, on Line 2 of the second window of code, instead of “myFoldOuts” it says “myFooldOuts.” Most people will likely discover this quickly, but I note it here just in case.

1 Like