I have an array of 13 dropdown boxes, each attached to an event in my game. I have AddListener events attached to each, and they all call the same routine which gets passed a variable designating which dropdown changed value. However, I’d like to also trap when a user just clicks on a dropdown, without changing a value. Here’s my code. I’m getting a compiler error on my OnPointerClick statement. I need a way to pass the dropdown that triggered the OnPointerClick event. (Or any other way of getting the selected dropdown.)
Thanks for any help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SelectSounds : MonoBehaviour
{
public TMPro.TMP_Dropdown[] dropdownSFX = new TMPro.TMP_Dropdown[13];
void Awake()
{
for (int i = 0; i < 13; i++)
{
var temp = i;
dropdownSFX[i].onValueChanged.AddListener(delegate { DropdownValueChanged(temp); }); // This works fine on value change
dropdownSFX[i].OnPointerClick.AddListener(delegate { DropdownValueChanged(temp); });
// Gets error: TMP_Dropdown.OnPointerClick(PointerEventData)' is a method, which is not valid in the given context
}
private void DropdownValueChanged(int dropdownSelected)
{
// Get the value of the dropdown, and from which dropdown it came?
Debug.Log("In DropdownVC: " + dropdownSelected);
}
}
I’ll answer my own question, should anyone find this in search of the same solution. I attached an EventHandler to each dropdown, then added an OnPointerClick event, pointing to the DropdownValueChanged method, and the dropdown box number in the pass parameter list.
Thank you for this Burlisoft. I’ll be more sfectific for this.
In Unity, you can add an EventTrigger component to the Dropdown game object, then add a new entry in the “Events” section of the EventTrigger component for the “PointerClick” event type. Finally, you can create a function in a script that will be called when the “PointerClick” event is triggered, and then assign the function as the target for the “PointerClick” event in the EventTrigger component.
public void OnPointerClickHandler(BaseEventData eventData)
{
// Your code here
}
The OnPointerClick() of Dropdown only triggers when the option is opening.