Version: 2018.2
public List<OptionData> options ;

Description

The 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); } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961