使用字符串统一设置下拉值

Hik*_*ari 4 c# unity-game-engine

有谁知道如何 使用统一的字符串设置dropdownUI 值?
我知道如何用这样的 int 设置它

public DropDown dropdown;

dropdown.value = 1;
Run Code Online (Sandbox Code Playgroud)

但是,我想要的是用给定的字符串设置值。

就像是:

dropdown.value = "an Option";
Run Code Online (Sandbox Code Playgroud)

der*_*ugo 12

查看Dropdown.value的文档:

public int value;

值是下拉列表中当前选择的索引号。0 是下拉列表中的第一个选项,1 是第二个选项,依此类推。

它的类型int,从而在短:你不能


但是,您可以使用以下方法从可用选项本身获取索引IndexOf

// returns a list of the text properties of the options
var listAvailableStrings = dropdown.options.Select(option => option.text).ToList();

// returns the index of the given string
dropdown.value = listAvailableStrings.IndexOf("an Option");
Run Code Online (Sandbox Code Playgroud)

你也可以在一行中使用 FindIndex

dropdown.value = dropdown.options.FindIndex(option => option.text == "an Option");
Run Code Online (Sandbox Code Playgroud)