- Home /
Best way to override OnClick with OnPointerDown for Toggle?
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.
Answer by Lo0NuhtiK · Mar 11, 2016 at 04:29 PM
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
Your answer
Follow this Question
Related Questions
Daikon Forge - Close windows when clicking out of bounds 0 Answers
OnClickUpAsButton used externaly 0 Answers
UI button stops working after reload scene 1 Answer
[4.6] Toggle/Checkbox On/Off 1 Answer
Change Scroll bar on click 0 Answers