Search code examples
c#unity-game-engineunity-editor

how to add items from a List to a GenericMenu (Editor Window) and you can only select one


I am working in Unity using the UnityEdior library. I made a custom editor window and I am filling in the void OnGui(). I created a list that takes all the string version of the names of the branches from my git repository like this

void OnGUI()
{
    Process checkbranch = new Process();
    if (GUILayout.Button("Show Branches"))
    {
        checkbranch.StartInfo.FileName = "git";
        checkbranch.StartInfo.Arguments = "branch";
        checkbranch.StartInfo.UseShellExecute = false;
        checkbranch.Start();
        checkbranch.WaitForExit();

        Branches.Clear();
        string branch;
        branch = checkbranch.StandardOutput.ReadLine();
        while (!string.IsNullOrEmpty(branch))
        {
           var m = new CheckBranch();
           m.BranchName = branch;
           Branches.Add(m);

           branch = checkbranch.StandardOutput.ReadLine();
         }
     }
}

public class CheckBranch
{
     public string BranchName;
     public bool BranchBool;
}

and now im trying to add these branches to the generic menu so there will be a drop down menu where you can chose the branch you want to switch to. So based on that statement it means that you can only select one branch from the generic menu.

Then I tried adding them to the menu and here where it starts to not work.

private void OnGui()
{
...
if (GUILayout.Button("test"))
        {
            GenericMenu menu = new GenericMenu();
            for (int i = 0; i < Branches.Count; i++)
            { 
                menu.AddItem(new GUIContent(Branches[i].BranchName), Branches[i].BranchBool, Toggle); 
            }
            menu.ShowAsContext();
        }
}
void Toggle()
    {
        Process p = new Process();

        for (int i = 0; i < Branches.Count; i++)
        {
            Branches[i].BranchBool = true;
            p.StartInfo.FileName = "git";
            p.StartInfo.Arguments = $"checkout {Branches[i].BranchName}"; // menu function
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.UseShellExecute = false;
            p.Start();
            p.WaitForExit();
        }
    }

So when I select one of the branches it selects all how do I work around this.

here are the photos from unity before pressing

after pressing

Please help.


Solution

  • I would try something more like a combo box. EditorGUILayout.Popup sounds exactly like what you are looking for.

    Try this in the GUI Layout:

    string[] options = new string[movParamsListSize];
            for (int i = 0; i < movParamsListSize; i++)
                options[i] = i.ToString();
    movStepToMoveTo.intValue = EditorGUILayout.Popup("movementStepToMoveTo",
          movStepToMoveTo.intValue, options, EditorStyles.popup);
    

    This is like a combo box with the options you specify in the options string[]. See movementStepToMoveTo field, which is a Serialized property:

    enter image description here

    Hope that is useful.