Add a dropdown inside OnGUI function

Viewed 4013

I build a menu inside the OnGUI function like

GUILayout.Label("Singleplayer");
if(GUILayout.Button("Zombie"))
    ChangeScene("Zombie");

Is it possible to add a Dropdown programmatically like i create the button?

1 Answers

If you're doing this in the editor - i.e. a custom Inspector or EditorWindow - then yes:

https://docs.unity3d.com/ScriptReference/EditorGUILayout.DropdownButton.html

But if you're doing it in-game, you need to stop using the OnGUI method (it's not supported any more by Unity) and switch to using the new UnityUI (which isn't really "new" any more - it went live almost 5 years ago! :)). UnityUI has built-in drop-down functionality you create directly in the Editor:

https://docs.unity3d.com/2018.4/Documentation/ScriptReference/UI.Dropdown.html

and:

https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/script-Dropdown.html

Related