In this article we’ll implement a difficulty choice with the Dropdown UI element.
Initial Scene Setup
Our scene is pretty simple, we have our customary Canvas with Image element background, a couple Text elements, and the TextMesh Pro (TMP) Dropdown element.
In previous versions of Unity there is a standard Dropdown element and a TMP version, but in Unity 2021+ there is only the TMP version.
Note that the child elements under “Template” would be where you’d want to customize color, font, etc as these elements will override the Label and Arrow elements.
This can be convenient as you can select premade Templates if you need a dropdown with a particular look and feel as another dropdown you have done prior.
On the Dropdown element itself we can set the names of each option.
We could also add sprites for each option if we so desired.
Let’s create a script and assign it to the Canvas element.
The DropdownDifficulty Class/Script
It doesn’t get much simpler than this.
We’ll hook up the OnSelectChoice() method to the On Value Changed Unity Event of the Dropdown element and spit out some text when the dropdown changes.
Next, we need to assign the elements to our script component.
Lastly, let’s tell our Dropdown element’s Unity Event for On Value Changed to call our OnSelectedChoice() method.
If we hit play, we should see something similar to the above.
See you in the next article!