Jump to content

A GUI DropDownList


Recommended Posts

I've been progressing my plugin rewrite and wanted to use a dropdownlist control, only to find that there isnt a native Unity one. Because I'm either persistent or stupid - the jury is still out - I fiddled around until I got the below working. Apologies for the audio and my dodgy voice over :P

Basically it is a reasonably simple dropdown control, you feed it a list of strings, but you do have to call a few functions in the right part of the draw routine so that things are ordered/layered correctly for it to function. You can look through the source code and see the plugin class, but here is the usage details from the above video

        void DrawWindow(Int32 id)
{
//this draws the transparent catching button that stops items behind the listbox getting events - and does the actual selection of listitems
//Its the first item in the draw function so its at the lowest layer, and therefore consumes button click events
ddlTest.DrawBlockingSelector();

//Layout Code here
GUILayout.BeginVertical();
GUILayout.Label("The dropdown should work and obscure the second button");

//This draws the button and displays/hides the listbox - returns true if someone clicked the button
ddlTest.DrawButton();

//Heres our button to avoid clicking
if (GUILayout.Button("Dont click this through list"))
ScreenMessages.PostScreenMessage("Clicked the Dont Button", 1, ScreenMessageStyle.UPPER_RIGHT);

GUILayout.EndVertical();

//These two go at the end so the dropdown is drawn over the other controls, and closes the dropdown if someone clicks elsewhere in the window
ddlTest.DrawDropDown();
ddlTest.CloseOnOutsideClick();

GUI.DragWindow();
}

The only thing to be aware of is that because it is drawing the dropdown inside the window if the list is bigger than the window the draw calls will be clipped.

I plan to use this in a few plugins and add some styling, selection icons, etc, but feel free to use any of this as you see fit (is in the same source code location as the Plugin Framework and licensed by MIT License).

Source Code

I'll probably write an extra blog post about how I threw it together a bit later.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...