Best way to override OnClick with OnPointerDown for Toggle? (160687)

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.

so i tried the debug.log and nothing happened. any other ideas?

2 Answers

2

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

You mean nothing get printed in the console? If that the case, it means UpdateState is not called. Maybe you should check StatePatternGameplay and see if it does not somehow change the currentState.

so i got the debug.log to work. my timer starts at 5 but it counts down to 4.98 so it does count down slightly but for some reason it doesn't seem to refresh the UpdateState. any ideas what could cause this? again thanks for your help. i will look into the behaviours trees too but i would really like to get this fixed

It seems it is called only once. It's likely that the currentState is changed. Maybe I could help further if you post the whole code, included the StatePatternGamplay class.

This code works on my side. The countdown is decreasing. Do you have any error in the console?

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