So I’m attempting to utilize Unity’s standard Dropdown UI element in an application I’m developing. Right now I’m trying to wire up sounds to play when my UI elements are clicked on, but it seems Unity’s Dropdown doesn’t have an onClick event to subscribe to. I’d hate to try to extend the Dropdown script and add the event myself, but it seems like that’s the only option available. Does anyone know of a potential workaround, and if not, maybe a clean way to go about doing this?
Found the answer to my question:
Currently my solution is to attach this to the actual Dropdown object:
Step 1: Add the Event Trigger component to the Game Object
Step 2: Create a C# script, I’m calling it DropdownExtension currently, with the code below.
using UnityEngine;
using UnityEngine.EventSystems;
public class DropdownExtension : MonoBehaviour
{
void Awake()
{
EventTrigger Trigger = GetComponent<EventTrigger>();
EventTrigger.Entry E = new EventTrigger.Entry();
E.eventID = EventTriggerType.PointerClick;
E.callback.AddListener(delegate { OnClick(); });
Trigger.triggers.Add(E);
}
void OnClick()
{
Debug.Log("I'VE BEEN CLICKED!");
}
}
Step 3. Attach it to the Dropdown Gameobject, and celebrate.