Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

simple drop down menu script for gui

Discussion in 'Immediate Mode GUI (IMGUI)' started by Dejan1, Jul 5, 2013.

  1. Dejan1

    Dejan1

    Joined:
    May 5, 2013
    Posts:
    13
    Here is an example of a dropdown select country script:

    Code (csharp):
    1.  
    2.  
    3.     private Vector2 scrollViewVector = Vector2.zero;
    4.     private string[] countrys = {"Any country","Afghanistan", "Albania", "Algeria"};//add the rest
    5.     int n,i,wichcountry;
    6.  
    7.     void Start () {
    8.      n=0;i=0;wichcountry=0;
    9.     }
    10.  
    11.     void OnGUI () {
    12.  
    13.      if(GUI.Button(new Rect(125,50,25,25), "")){
    14.       if(n==0)n=1;
    15.       else n=0;        
    16.      }
    17.  
    18.      if(n==1){
    19.        scrollViewVector = GUI.BeginScrollView (new Rect (25, 50, 100, 115), scrollViewVector, new Rect (0, 0, 300, 500));
    20.        GUI.Box(new Rect(0,0,300,500), ""); 
    21.        for(i=0;i<4;i++){
    22.         if(GUI.Button(new Rect(0,i*25,300,25), "")){
    23.          n=0;wichcountry=i;        
    24.         }              
    25.         GUI.Label(new Rect(5,i*25,300,25), countrys[i]);           
    26.        }
    27.        GUI.EndScrollView();        
    28.       }else{
    29.        GUI.Label(new Rect(30,50,300,25), countrys[wichcountry]);
    30.       }            
    31.     }
    32.  
    33.  
    make sure you make no buttons behind scroll view rectangle.
     
    Psyco92 likes this.
  2. LightningDragon

    LightningDragon

    Joined:
    Jun 18, 2013
    Posts:
    6
    Thanks this helped me a lot. I made some changes to make it more dynamic, such as, the scroll bars won't pop up unless the number of items exceeds the default size, and you can move it by changing a Vector2.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class NewBehaviourScript : MonoBehaviour
    5. {
    6.     private Vector2 scrollViewVector = Vector2.zero;
    7.     public Rect dropDownRect = new Rect(125,50,125,300);
    8.     public static string[] list = {"Drop_Down_Menu"};
    9.    
    10.     int indexNumber;
    11.     bool show = false;
    12.  
    13.     void OnGUI()
    14.     {  
    15.         if(GUI.Button(new Rect((dropDownRect.x - 100), dropDownRect.y, dropDownRect.width, 25), ""))
    16.         {
    17.             if(!show)
    18.             {
    19.                 show = true;
    20.             }
    21.             else
    22.             {
    23.                 show = false;
    24.             }
    25.         }
    26.        
    27.         if(show)
    28.         {
    29.             scrollViewVector = GUI.BeginScrollView(new Rect((dropDownRect.x - 100), (dropDownRect.y + 25), dropDownRect.width, dropDownRect.height),scrollViewVector,new Rect(0, 0, dropDownRect.width, Mathf.Max(dropDownRect.height, (list.Length*25))));
    30.            
    31.             GUI.Box(new Rect(0, 0, dropDownRect.width, Mathf.Max(dropDownRect.height, (list.Length*25))), "");
    32.            
    33.             for(int index = 0; index < list.Length; index++)
    34.             {
    35.                
    36.                 if(GUI.Button(new Rect(0, (index*25), dropDownRect.height, 25), ""))
    37.                 {
    38.                     show = false;
    39.                     indexNumber = index;
    40.                 }
    41.                
    42.                 GUI.Label(new Rect(5, (index*25), dropDownRect.height, 25), list[index]);
    43.                
    44.             }
    45.            
    46.             GUI.EndScrollView();   
    47.         }
    48.         else
    49.         {
    50.             GUI.Label(new Rect((dropDownRect.x - 95), dropDownRect.y, 300, 25), list[indexNumber]);
    51.         }
    52.        
    53.     }
    54.  
    55.  
    56.  
     
    Last edited: Jul 7, 2013
    sntilt likes this.
  3. Dejan1

    Dejan1

    Joined:
    May 5, 2013
    Posts:
    13
    Much better.
    Everyone - use this in your script instead :)
    Also, don`t forget NOT to put any buttons behind scrollview rectangle.
     
  4. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    And that is because of the... scrollview bug. :)
     
  5. shahad_s1l

    shahad_s1l

    Joined:
    Jan 25, 2021
    Posts:
    3
    sorry but what should i put ? i have inputField i want the date to show on
    should i only but a button or inputField