PopupList

Author: Eric Haines (Eric5h5)

Description
Creates a popup list button as an extension to OnGUI. When the button is clicked, a selectable list pops up, and disappears when the mouse button is released.



Usage
Put the Popup.cs script below in your Standard Assets/Scripts folder (if you don't have one, make one). This way it can be accessed from any language easily without having to worry about compilation order problems. You can then access the List function by calling Popup.List, which must be called from within OnGUI.

When called, it returns true if the user has selected an item from the list and false otherwise. It requires an existing boolean to be passed in, which reflects whether the list is currently being shown or not. It also requires an existing integer to be passed in, which is the index of the list entry that's selected when the user releases the mouse button.

function List (position : Rect, ref showList : boolean, ref listEntry : int, buttonContent : GUIContent, listContent : GUIContent[], buttonStyle : GUIStyle, boxStyle : GUIStyle, listStyle : GUIStyle) : boolean

position is the rect where the button should be located. showList is a boolean that's true if the list is being shown. This is a reference boolean, so when used with C#, it must be preceded with "ref". listEntry is an integer that holds the value of the list entry that the user selected. This is a reference integer, so when used with C#, it must be preceded with "ref". buttonContent is the GUIContent that's displayed on the button. listContent is a GUIContent array that contains the list that's displayed when the button is clicked.

Of the last three GUIStyle variables, the first two are optional. If they are not supplied, the current "button" and "box" styles from the current GUI skin are used. Otherwise, buttonStyle is the GUIStyle for the button, and boxStyle is the GUIStyle for the box that the list is displayed in. listStyle is not optional, and is the style used for displaying the list contents.

Note that the default Unity GUI skin isn't entirely suitable for this function. The GUIStyle used for the list needs to have a texture background for the Hover and OnHover entries, so that the user has some kind of visual feedback as to which item in the list will be selected, and none of the default styles look particularly good for this ("button" might do in a pinch). However, it's pretty easy to add a custom style, and a small solid-colored texture will do nicely for the background. See the usage example below for an example of this. Also remember that GUIContent can have both text and an image, so the button and list can have associated icons if desired (see screenshot at the top of this article).

Update John H. (Kentu) 8/11/2010

I changed this C# script to accept an abstract array, and also be able to assign a delegate upon mouse up. To pass in a List you would for example do this YourList.ToArray simple right. You will also want to pass in your function as the delegate callback. This can be extended to not take a callback delegate, this is just mainly for example. For the item to return the correct name, the objects you are creating your class/List from, you just simple Overide the ToString method, Example code file below.

Update Hyungseok Seo. (Jerry) 28/12/2010

I changed this C# code, because when I clicked this button, I want to see seperate gui for ComboBox and List. (you can see the result from Decription's last picture). And also I want to use PopupList like ComboBox control. That mean's control have some data. (for example, selected item index.). so you don't need to care for that kind of data anymore, when you want to use PopupList.

Update MarkGX, 13/01/2011

The ComboBox has an error where you have to click it twice before it appears with the drop down. Here is the code fix

Update Jerry 25/01/2011

Fixed bug. - Thanks MarkGX. :)

C# - ComboBox - Update
I just refactored Hyungseok Seo.(Jerry) sdragoon@nate.com's ComboBox to oop. I don't know how the "button" and "box" string be converted to GUIStyle, so leave the constructor's prameter as "string buttonStyle" and "string boxStyle".

C# Example Class
I just wanted the button to automatically show what you have selected instead of a label