Version: 2017.3

Dropdown.onValueChanged

切换到手册
public UI.Dropdown.DropdownEvent onValueChanged ;

描述

在用户单击了下拉列表中一个选项时调用的 UnityEvent。

可以用来检测用户选择下拉列表中的一个或多个选项的时间。添加一个监听器,当此 UnityEvent 检测到用户进行选择时执行操作。请参阅委托教程了解有关委托的更多信息。

//Create a new Dropdown GameObject by going to the Hierarchy and clicking Create>UI>Dropdown. Attach this script to the Dropdown GameObject.
//Set your own Text in the Inspector window

using UnityEngine; using UnityEngine.UI;

public class Example : MonoBehaviour { Dropdown m_Dropdown; public Text m_Text;

void Start() { //Fetch the Dropdown GameObject m_Dropdown = GetComponent<Dropdown>(); //Add listener for when the value of the Dropdown changes, to take action m_Dropdown.onValueChanged.AddListener(delegate { DropdownValueChanged(m_Dropdown); });

//Initialise the Text to say the first value of the Dropdown m_Text.text = "First Value : " + m_Dropdown.value; }

//Ouput the new value of the Dropdown into Text void DropdownValueChanged(Dropdown change) { m_Text.text = "New Value : " + change.value; } }