PointerDownEvent on Buttons not working

Hi,
Using Unity 2021.2.3, I wonder why PointerDownEvent or MouseDownEvent are not called on buttons upon a mouse click or SendEvent. Is it a bug? MouseDownEvent do is called on a VisualElement upon a SendEvent with PointerDownEvent. I’d expect it to work on a button as well. ClickEvent act as mouse/pointer up, but I need the down callback.

Thanks

Hi,

The Button indeed deals with ClickEvents, via its clickable manipulator. In order to be able to get the PointerDownEvents you need to register in TrickeDown mode:

var button = ...; // or var button = root.Q<Button>(className: "someButton"), etc.
button.RegisterCallback<PointerDownEvent>(
    e => {
        Debug.Log("Button pointer down!");
    },
    TrickleDown.TrickleDown);

Hope that helps!

9 Likes

Wish I’d figure this by myself. This is a weird design in my opinion to specify “down” twice. Why are we even allowed to register a callback with down and up at the same time which does nothing? Perhaps a warning could be printed in the log? It works, let’s move on, thanks.

Hi,
First of all, glad it fixes your problem.
Second, I understand your point – unfortunately it’s just that the Button element is a special snowflake and was written a while ago. Clickable is just a shortcut for the more simple cases and that, in retrospect, would probably not be our approach today. But that said, the clickable callback you are referring to should be doing something, unless you did not specify a callback when creating the Button instance.

This trick does not work for me in 2023.1
button.RegisterCallback(HandleClicked, TrickleDown.TrickleDown); does not work
button.RegisterCallback(HandleClicked) works

I confirm it works! Thanks from 2024

This took me a while to figure out. I need to learn more about event propagation . Adding the trickle down flagged worked for me but I still have to clue why. All of my buttons are on the same layer as far as I know

Will this syntax / behaviour change at some point that the PointerDownEvent is cancelled for Buttons in the TrickleUp phase? (at least it seems that PointerDownEvents on Buttons are cancelled in TrickleUp phase if one has to restrict the event registration to TrickleDown phase)

If not it would be cool to have a comment in the documentation, that the behaviour / systax for buttons is different (special) compared to that of a usual Visual Elements like

visualElement.RegisterCallback<PointerDownEvent>(FunctionCall);
button.RegisterCallback<PointerDownEvent>(FunctionCall, TrickleDown.TrickleDown);

Maybe one could add the optional TrickleDown argument in the example given on that page to make it easier to get the solution.

I doubt it will change. It can safely be assumed that any control that responds to a particular event/input will stop its propagation. Very similar to how IMGUI would work in most cases as well.

If unsure you can always find it in the source code reference: UnityCsReference/Modules/UIElements/Core/Clickable.cs at master · Unity-Technologies/UnityCsReference · GitHub

Though I do think the default parameter for the trickle down in RegisterCallback being BubbleUp was a bit of a mistake.