Search Unity

Simple drop-down list

Discussion in 'Assets and Asset Store' started by enyllief, Jul 23, 2012.

  1. enyllief

    enyllief

    Joined:
    Oct 17, 2010
    Posts:
    36
    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: http://www.unifycommunity.com/wiki/index.php?title=PopupList

    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:
    Code (csharp):
    1. //global variables for settings
    2. var showList                    : boolean   = false;
    3. static var listEntrySelected    : int;
    4. static var listEntry            : int       = 2;
    5. static var defaultEntryNumber   : int       = 0;
    6. var generalListStyle            : GUIStyle  = new GUIStyle();
    7.  
    8. //dropdown menu content
    9. var listColours : GUIContent[]; listColours = new GUIContent[5];
    10. listColours[0]  = new GUIContent("Blue");
    11. listColours[1]  = new GUIContent("White");
    12. listColours[2]  = new GUIContent("Red");
    13. listColours[3]  = new GUIContent("Green");
    14. listColours[4]  = new GUIContent("Purple");
    15.  
    16. generalListStyle.padding.left = generalListStyle.padding.right = generalListStyle.padding.top = generalListStyle.padding.bottom = 4;
    17.  
    18. var dropdownListHash : int = "DropdownList".GetHashCode();
    19.  
    20. //      List(Rect(0,0,100,100),         false,              0,              GUIContent("Select Colour"),    listColours             "button",               "box",          generalListStyle)
    21. function List(position : Rect, expandList : boolean, listEntry : int, defaultListEntry : GUIContent, listToUse : GUIContent[], buttonStyle : GUIStyle, boxStyle : GUIStyle, listStyle : GUIStyle)
    22. {
    23.    
    24.     controlID = GUIUtility.GetControlID(dropdownListHash, FocusType.Passive);
    25.     var done : boolean = false;
    26.    
    27.     if(Event.current.GetTypeForControl(controlID) == EventType.mouseDown)
    28.     {
    29.     if (position.Contains(Event.current.mousePosition))
    30.         {
    31.         GUIUtility.hotControl = controlID;
    32.         showList = !showList;
    33.         }
    34.     }
    35.    
    36.     if(Event.current.GetTypeForControl(controlID) == EventType.mouseDown  !position.Contains(Event.current.mousePosition))
    37.     {
    38.         GUIUtility.hotControl = controlID;
    39.     }              
    40.    
    41.     GUI.Label(position, defaultListEntry, buttonStyle);
    42.    
    43.     if(expandList)
    44.     {
    45.     //list rectangle
    46.     var listRect : Rect = new Rect(position.x, position.y+20, position.width, listStyle.CalcHeight(listToUse[0], 1.0f) * listToUse.Length);
    47.     GUI.Box(listRect, "", boxStyle);
    48.    
    49.     listEntrySelected = GUI.SelectionGrid(listRect, listEntrySelected, listToUse, 1, listStyle);
    50.     listEntry = listEntrySelected;
    51.    
    52.         if(listEntrySelected != defaultEntryNumber  !position.Contains(Event.current.mousePosition))
    53.         {
    54.         GUIUtility.hotControl = controlID;
    55.         showList = !showList;
    56.         defaultEntryNumber = listEntrySelected;
    57.         }    
    58.     }
    59. }
    60.  
    61. function OnGUI()
    62. {
    63.     GUI.Label(Rect(200,200, 100, 20), listColours[listEntrySelected].text);
    64.     List(Rect(50, 100, 100, 20), showList, listEntry, GUIContent(listColours[listEntrySelected].text), listColours, "button", "box", generalListStyle);
    65. }
    You can customise generalListStyle in the Unity3d editor and use List function to pass your list to the function.
     
    Last edited: Jul 23, 2012
  2. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Thanks. It looks like a promising script. But how would I trigger a function or store information once a option has been selected? Such as changing a game mode?

    Also, how would I make the buttons highlight when they are being cursed over?
     
    Last edited: Jul 25, 2012
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I would strongly recommend not putting everything in one file. It should be done like the original, where List is a static function in its own class. This way it can be called by any OnGUI function.

    --Eric
     
  4. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    The other one though the mouse button needs to be held down in order to see the list. Also the button that needs to be clicked to see the list, it does not change its string when selecting another option in the menu. It just remains as ''button''.

    I would just go in and change the script but the methods used are a little advanced for me.
     
  5. enyllief

    enyllief

    Joined:
    Oct 17, 2010
    Posts:
    36
    You can use either
    listColours[listEntrySelected].text
    or
    listColours[listEntrySelected]

    to assign it to your variable.

    You can customise your GUI style:


    Thanks for your script.

    Originally the JS rewrite of it was designed to work with NecromancerGUI: http://forum.unity3d.com/threads/80174-Necromancer-GUI-Skin
    Everything (main menu, pause menu, interface etc.) in one file.

    Somebody can still rewrite this and move the List function plus variables to a separate JavaScript file.
     
  6. mcconrad

    mcconrad

    Joined:
    Jul 16, 2013
    Posts:
    155
    this gives several errors when attempting to compile in latest version of unity:

    (36,75): BCE0044: expecting ), found '!'.
    (36,122): BCE0043: Unexpected token: ).
    (52,53): BCE0044: expecting ), found '!'.
    (52,100): BCE0043: Unexpected token: ).

    those lines are either missing && or || or such.
     
    B_Dombro likes this.
  7. B_Dombro

    B_Dombro

    Joined:
    Oct 7, 2013
    Posts:
    1
    ^^^ They are both &&.