Search Unity

Dropdown selection list?

Discussion in 'Immediate Mode GUI (IMGUI)' started by seon, Jan 3, 2008.

  1. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    Anyone got one of these happening yet? Shaun? Bueller.. Bueller ?
     
  2. yellowlabrador

    yellowlabrador

    Joined:
    Oct 20, 2005
    Posts:
    562
    Not sure exactly what kind of drop down, but this is what I was trying to do.

    Not exactly the way I wanted it, trying to figure out how to not let the first GUI show after the bottom screen rolls down again to show the credit GUI.

    See attached .mov

    Ray
     

    Attached Files:

  3. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    thats cool, but I am talking about an actual dropdown selection list like in a webpage.

    I'm sure UT will eventually make one, but I am hoping to implement selection with a selection list before that may happen.
     
  4. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Yeh, I wrote a dropdown list ages ago, but I lost it. So, I made a new one just for you :D
    It's a ugly as hell - but it works. Hope this helps.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour
    6. {
    7.     private Rect DropDownRect;
    8.     private Vector2 ListScrollPos;
    9.     private bool DropdownVisible;
    10.     private int SelectedListItem;
    11.     public class GuiListItem //The class that contains our list items
    12.     {
    13.         public bool Selected;
    14.         public string Name;
    15.         public GuiListItem(bool mSelected, string mName)
    16.         {
    17.             Selected = mSelected;
    18.             Name = mName;
    19.         }
    20.         public GuiListItem(string mName)
    21.         {
    22.             Selected = false;
    23.             Name = mName;
    24.         }
    25.         public void enable()
    26.         {
    27.             Selected = true;
    28.         }
    29.         public void disable()
    30.         {
    31.             Selected = false;
    32.         }
    33.     }
    34.     private List<GuiListItem> MyListOfStuff; //Declare our list of stuff
    35.     void Start()
    36.     {
    37.  
    38.         DropDownRect = new Rect(100, 300, 160, 28);//We need to manually position our list, because the dropdown will appear over other controls
    39.         DropdownVisible = false;
    40.         SelectedListItem = -1;
    41.         MyListOfStuff = new List<GuiListItem>(); //Initialize our list of stuff
    42.         for (int i = 0; i < 32; i++)//Fill it with some stuff
    43.         {
    44.             MyListOfStuff.Add(new GuiListItem("Item Number" + i.ToString()));
    45.         }
    46.     }
    47.  
    48.     void OnGUI()
    49.     {
    50.         //Show the dropdown list if required (make sure any controls that should appear behind the list are before this block)
    51.         if (DropdownVisible)
    52.         {
    53.             GUILayout.BeginArea(new Rect(DropDownRect.left, DropDownRect.top + DropDownRect.height, 160, 256), "", "box");
    54.             ListScrollPos = GUILayout.BeginScrollView(ListScrollPos, false, true);
    55.             GUILayout.BeginVertical(GUILayout.Width(120));
    56.             for (int i = 0; i < MyListOfStuff.Count; i++)
    57.             {
    58.                     if (!MyListOfStuff[i].Selected  GUILayout.Button(MyListOfStuff[i].Name))
    59.                     {
    60.                         if (SelectedListItem != -1) MyListOfStuff[SelectedListItem].disable();//Turn off the previously selected item
    61.                         SelectedListItem = i;//Set the index for our currrently selected item
    62.                         MyListOfStuff[SelectedListItem].enable();//Turn on the item we clicked
    63.                         DropdownVisible = false; //Hide the list
    64.                     }
    65.             }
    66.             GUILayout.EndVertical();
    67.             GUILayout.EndScrollView();
    68.             GUILayout.EndArea();
    69.         }
    70.         //Draw the dropdown control
    71.         GUILayout.BeginArea(DropDownRect, "", "box");
    72.         GUILayout.BeginHorizontal();
    73.         string SelectedItemCaption = (SelectedListItem == -1) ? "Select an item..." : MyListOfStuff[SelectedListItem].Name;
    74.         string ButtonText = (DropdownVisible) ? "<<" : ">>";
    75.         GUILayout.TextField(SelectedItemCaption);
    76.         DropdownVisible = GUILayout.Toggle(DropdownVisible, ButtonText, "button", GUILayout.Width(32), GUILayout.Height(20));
    77.         GUILayout.EndHorizontal();
    78.         GUILayout.EndArea();
    79.     }
    80. }
     
  5. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    Just whipped it up hey Shaun...? Man, you are a legend! Thanks muchly!
     
  6. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    heh well yea, kind of - I wanted to try using a generics list with my own faux listitem class - my old dropdown was S***tily programmed :oops:

    Ah - there's a couple of caveats; It won't respect GUILayout very well, and if the list goes outside of the GUI.Window, it will be clipped (which only matters if you use windows anyway). Also try it first with the default GUISkin - it will look normal. And, if you want to do MultiSelect (is that what its called?) - just turn of the code that disables the item and the condition for showing the button (lines 58 and 60)
     
  7. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    Thanks Shaun... sounds ideal.. I really only want it at the moment so people can choose in game what country they are from from a list of options in a drop down... so this will work gr8!