I want to execute a toggle’s OnValueChanged method on a OnPointerDown event instead of an OnClick event. Is there an elegant way to do this?
Right now I’m just using an EventTrigger component with a OnPointerDown event on the Toggle GameObject to execute the the method I want, but this gets gross quickly. For one OnClick is still called on the Toggle Component. Yes, I could turn interactable false on the Toggle component, but then this presents the wrong visual state, yada yada yada.
I really just want all the functionality of the toggle, I just want it to be triggered OnPointerDown INSTEAD of OnClick.
Make a class that inherits from Toggle and override its stuff…
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class MyToggle : Toggle
{
public override void OnPointerClick(PointerEventData eventData)
{
//do nothing... we don't want it do do anything OnClick
}
public override void OnPointerDown(PointerEventData eventData)
{
//do the stuff it usually does
base.OnPointerDown(eventData);
//do the stuff we want it to do
base.OnSubmit(eventData);
}
}
Then take the unity toggle component off, put this on, reassign the target graphic etc.
What do you mean with: “Then take the unity toggle component off, put this on, reassign the target graphic etc.”, please explain, I’m new to unity