Unity Dropdown add values from database

2.7k views Asked by At

I want to display values from a database to a drop-down list; how I can achieve this?

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));    
}   

I want to make these dynamic or sync with live db values.

1

There are 1 answers

0
eagle On BEST ANSWER
List<string> list = new List<string> { "option1", "option2" };
var dropdown = GetComponent<Dropdown>();
dropdown.ClearOptions(); // better approach
dropdown.AddOptions(list); // this is your required solution

I will also suggest, it is good to have the reference of dropdown via-inspector, instead of GetComponent. As GetComponent call is expensive!

Late answer, might be it would save someone's time!