Mouseover on dropdown list
Hi, when I use Dropdown on unity UI, a DropDown list is created with all the options that can be selected.
I was wondering if there was any way, we could know which option is currently being mouse-over?
Thanks
Answer by Naphier · Nov 14, 2016 at 10:10 PM
Check out the EventTrigger component. The dropdown component has a Template Structure with an "Item" in it, this is like the prefab for the dropdown items which are created at runtime. You'll attach the EventTrigger component to the Item and a script that will identify the dropdown item being hovered over (OnPointerEnter event).
Here's a shot of the setup:
And here's the EventTriggerTest.cs script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class EventTriggerTest : MonoBehaviour
{
public Text label;
public Toggle toggle;
public void WhoAmI()
{
if (!label || !toggle)
{
Debug.LogError("please assign both the label and the toggle in the inspector");
return;
}
Debug.LogFormat("label: {0} toggle.isOn: {1}", label.text, toggle.isOn);
}
}
The script (like all other UI event scripts) does not necessarily need to be attached to the same game object as the Event Trigger.
Answer by mgstauff · Apr 05, 2020 at 02:33 PM
This is great!
And a way to also get the item info more directly in your handler is to make you event handler like so:
public void OnDropdownItemHighlighted(GameObject dropdownItem) //or Text dropdownItemText, etc.
{
//do whatever you want with the item
}