Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically change Dropdown Menu Options in Unity3D

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

like image 234
Hunselfunsel Avatar asked Oct 26 '15 13:10

Hunselfunsel


1 Answers

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));
}
like image 145
Hyster Avatar answered Oct 02 '22 00:10

Hyster