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

Report article 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: 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:

javascript code:
//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);
 
  if(expandList)
  {
  //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.

Post comment Comments
Guest
Guest Jun 11 2014, 1:41pm says:

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

Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Established
Oct 31, 2010
Privacy
Public
Subscription
Open to all members
Homepage
Unity3d.com
Contact
Send Message
Email
Members Only
Membership
Join this group
Group Watch
Track this group
Tutorial
Browse
Tutorials
Report Abuse
Report article
Related Games
Tribal Lands
Tribal Lands Indie Single Player Real Time Strategy
Related Engines
Unity
Unity Commercial Released May 30, 2005
Related Groups
Indie Devs
Indie Devs Hobbies & Interests group with 1,095 members
Unity devs
Unity devs Hobbies & Interests group with 1,184 members