Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseThe list of possible options. A text string and an image can be specified for each option.
This is the list of options within the Dropdown. Each option contains Text and/or image data that you can specify using OptionData before adding to the Dropdown list. This also unlocks the ability to edit the Dropdown, including the insertion, removal, and finding of options, as well as other useful tools.
//Create a new Dropdown GameObject by going to the Hierarchy and clicking Create>UI>Dropdown. Attach this script to the Dropdown GameObject.
using UnityEngine; using UnityEngine.UI; using System.Collections.Generic;
public class Example : MonoBehaviour { //Use these for adding options to the Dropdown List Dropdown.OptionData m_NewData, m_NewData2; //The list of messages for the Dropdown List<Dropdown.OptionData> m_Messages = new List<Dropdown.OptionData>();
//This is the Dropdown Dropdown m_Dropdown; string m_MyString; int m_Index;
void Start() { //Fetch the Dropdown GameObject the script is attached to m_Dropdown = GetComponent<Dropdown>(); //Clear the old options of the Dropdown menu m_Dropdown.ClearOptions();
//Create a new option for the Dropdown menu which reads "Option 1" and add to messages List m_NewData = new Dropdown.OptionData(); m_NewData.text = "Option 1"; m_Messages.Add(m_NewData);
//Create a new option for the Dropdown menu which reads "Option 2" and add to messages List m_NewData2 = new Dropdown.OptionData(); m_NewData2.text = "Option 2"; m_Messages.Add(m_NewData2);
//Take each entry in the message List foreach (Dropdown.OptionData message in m_Messages) { //Add each entry to the Dropdown m_Dropdown.options.Add(message); //Make the index equal to the total number of entries m_Index = m_Messages.Count - 1; } }
//This OnGUI function is used here for a quick demonstration. See the UI Section for more information about setting up your own UI. void OnGUI() { //TextField for user to type new entry to add to Dropdown m_MyString = GUI.TextField(new Rect(0, 40, 100, 40), m_MyString);
//Press the "Add" Button to add a new entry to the Dropdown if (GUI.Button(new Rect(0, 0, 100, 40), "Add")) { //Make the index the last number of entries m_Index = m_Messages.Count; //Create a temporary option Dropdown.OptionData temp = new Dropdown.OptionData(); //Make the option the data from the TextField temp.text = m_MyString;
//Update the messages list with the TextField data m_Messages.Add(temp);
//Add the Textfield data to the Dropdown m_Dropdown.options.Insert(m_Index, temp); }
//Press the "Remove" button to delete the selected option if (GUI.Button(new Rect(110, 0, 100, 40), "Remove")) { //Remove the current selected item from the Dropdown from the messages List m_Messages.RemoveAt(m_Dropdown.value); //Remove the current selection from the Dropdown m_Dropdown.options.RemoveAt(m_Dropdown.value); } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thanks for helping to make the Unity documentation better!