For all Unity developers and developers-to-be, both beginners and professionals!

Post tutorial Report content RSS feed Simple drop-down menu script for Unity3d

A simple drop-down menu script for Unity3d.

Posted by feillyne on Jul 23rd, 2012
Basic UI/HUD.

Since Unity3d doesn't have drop-down lists built in, here's a simple drop-down menu script heavily based on Eric Haines's PopupListUsageExample.js and Popup.cs from UnifyCommunity:

Main differences:
- instead of mouseDown and mouseUp events it uses two mouseDown events (so you click to expand the list and then click the option to select)
- the C# part of it is completely converted to JavaScript
- in one single file instead of two

All in one JavaScript file, you just need to add it to your JavaScript file:

//global variables for settings
var showList 					: boolean 	= false;
static var listEntrySelected 	: int;
static var listEntry 			: int 		= 2;
static var defaultEntryNumber 	: int		= 0;
var generalListStyle 			: GUIStyle 	= new GUIStyle();

//dropdown menu content
var listColours : GUIContent[]; listColours = new GUIContent[5];
listColours[0] 	= new GUIContent("Blue");
listColours[1] 	= new GUIContent("White");
listColours[2] 	= new GUIContent("Red");
listColours[3] 	= new GUIContent("Green");
listColours[4] 	= new GUIContent("Purple");

generalListStyle.padding.left = generalListStyle.padding.right = = generalListStyle.padding.bottom = 4;

var dropdownListHash : int = "DropdownList".GetHashCode();

//		List(Rect(0,0,100,100), 		false, 				0, 				GUIContent("Select Colour"), 	listColours				"button",				"box",			generalListStyle)
function List(position : Rect, expandList : boolean, listEntry : int, defaultListEntry : GUIContent, listToUse : GUIContent[], buttonStyle : GUIStyle, boxStyle : GUIStyle, listStyle : GUIStyle)
	controlID = GUIUtility.GetControlID(dropdownListHash, FocusType.Passive);
	var done : boolean = false;
	if(Event.current.GetTypeForControl(controlID) == EventType.mouseDown)
	if (position.Contains(Event.current.mousePosition))
        GUIUtility.hotControl = controlID;
       	showList = !showList;
   	if(Event.current.GetTypeForControl(controlID) == EventType.mouseDown && !position.Contains(Event.current.mousePosition))
		GUIUtility.hotControl = controlID;
	GUI.Label(position, defaultListEntry, buttonStyle);
	//list rectangle
	var listRect : Rect = new Rect(position.x, position.y+20, position.width, listStyle.CalcHeight(listToUse[0], 1.0f) * listToUse.Length);
	GUI.Box(listRect, "", boxStyle);
	listEntrySelected = GUI.SelectionGrid(listRect, listEntrySelected, listToUse, 1, listStyle);
	listEntry = listEntrySelected;
		if(listEntrySelected != defaultEntryNumber && !position.Contains(Event.current.mousePosition))
		GUIUtility.hotControl = controlID;
       	showList = !showList;
       	defaultEntryNumber = listEntrySelected;

function OnGUI()
	GUI.Label(Rect(200,200, 100, 20), listColours[listEntrySelected].text);
	List(Rect(50, 100, 100, 20), showList, listEntry, GUIContent(listColours[listEntrySelected].text), listColours, "button", "box", generalListStyle);

Important note: you have to replace "& amp;& amp;" with two ampersands "&&" IndieDB/ModDB html editor converts them automatically to html code. Here's a clean script:

You can customise generalListStyle in the Unity3d editor and use List function to pass your list to the function.

Post comment Comments
Guest Jun 11 2014 says:

This comment is currently awaiting admin approval, join now to view.

Guest May 21 2015 says:

This comment is currently awaiting admin approval, join now to view.

Post a comment

You are not logged in, your comment will be anonymous unless you join the community. Or sign in with your social account:

Oct 31, 2010
Open to all members
Send Message
Join this group
Group watch
Start tracking
Related Games
Tribal Lands
Tribal Lands Real Time Strategy
Related Engines
Unity Commercial Released 2005
Related Groups
Indie Devs
Indie Devs Hobbies & Interests with 1,285 members
Unity Devs
Unity Devs Hobbies & Interests with 1,447 members