• Register
Post tutorial Report RSS Simple drop-down menu script for Unity3d

A simple drop-down menu script for Unity3d.

Posted by on - 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: Unifycommunity.com

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.top = 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: Forum.unity3d.com

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


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


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

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.

Related Games
Tribal Lands
Tribal Lands Real Time Strategy
Related Engines
Unity Commercial
Related Groups
Indie Devs
Indie Devs Hobbies & Interests with 1,642 members
Unity Games
Unity Games Hobbies & Interests with 1,794 members