Programmatically change Dropdown Menu Options in Unity3D

Hunselfunsel picture Hunselfunsel · Oct 26, 2015 · Viewed 19.6k times · Source

i got Unity3D 5.2 and use the Dropdown GUI element for the first time. It is easy to use in the insepctor but i want the Options to show, be dependent of my files in Resources/Shapes/

So the dropdown should show all filenames i got in Resources/Shapes/ but i can not get a hold of this property in the attached C# script. After reading the manual on Dropdown, there should be a property with the name "Options" and it should have a string and an image variable. (So for my understanding its a two dimensional array-ish type)

Unfortunately i cant use the following script (pseudo code since it doesnt work)

GameObject obj = GameObject.Find("Dropdown");

var info = new DirectoryInfo("Assets/Resources/Shapes");
var fileInfo = info.GetFiles();
foreach (var file in fileInfo)
{
    //Add OptionsString Pseudo-Code
    obj.Options += file; // Options doesnt exist
}

Can anyone explain to me how i can manipulate the Options property on my Dropdown menu pls, i cant find anything in google. Only old ways from before the time Unity had a built in Dropdown menu

Thanks in advance

Answer

Hyster picture Hyster · Nov 25, 2015
List<string> list = new List<string> { "option1", "option2" };
var dropdown = GetComponent<Dropdown>();
dropdown.options.Clear();
foreach (string option in list)
{
    dropdown.options.Add(new Dropdown.OptionData(option));
}