UI Dropdown problem.
Hi, I am trying to understand the UI dropdown and On Value Changed. I understand the "on click" in UI Button but here I do not know what should I do. Let's take for example that I have dropdown with 3 options:
English
Spanish
Polish
And I have my
int english,
int spanish
int polish
I want them ( ints) to change when I choose one of languages. english =1,spanish=0,polish=0 etc. How to do it? Thanks.
Answer by Free_Radical · Apr 29, 2016 at 10:53 PM
Been doing a bit of work with the Dropdown myself lately. Still trying to master it. For your solution:
public GameObject YourDropdown;
// Assuming you want to initialize as soon as your object loads into the scene
void Awake()
{
AddListenerToDropdown();
}
// Add a listener to the Dropdown's OnValueChange event
void AddListenerToDropdown()
{
// Grab a reference to the Dropdown component
Dropdown Drop = YourDropdown.GetComponent<Dropdown>();
// Add a listener to the event. Yes, it probably looks weird if you're not used to
// C# events and delegates.
Drop.onValueChanged.AddListener(delegate { OnDropdownSelect(Drop); });
}
// What happens when a dropdown option is selected is handled here
void OnDropdownSelect(Dropdown Drop)
{
// Dropdown options are stored as ints, starting at 0. The value
// property is the value the dropdown is now set to when you selected
// a new option.
switch (Drop.value)
{
case 0: // The first option was selected
// Do Something
break;
case 1: // The second option was selected
// Do Something
break;
case 2: // The third option was selected
// Do Something
break;
default: // Should never happen
break;
}
}
Hope this helps.
Your answer
Follow this Question
Related Questions
TMP Dropdown resets its value 0 Answers
Best way to simplify this code 1 Answer
All transform.position has the same value in the Start method 0 Answers
No Script option in component Menu?! (Unity 5.3.1) 3 Answers
Better way to edit RawImage texture data?,Better way to edit a RawImage texture? 0 Answers