Search Unity

How do I detect right clicks at runtime?

Discussion in 'UI Toolkit' started by threedots1, Jun 9, 2020.

  1. threedots1

    threedots1

    Joined:
    Oct 9, 2014
    Posts:
    88
    For the life of me can't figure this out.

    I've registering a callback for ClickEvent, which works fine, I can detect double clicks etc etc.

    However ContextClickEvent never ever fires, unless I do so in the editor rather than inside the game window.

    I've tried using ContextualMenuManipulator, but that has the same problem.

    I feel like I'm missing something simple here, and shouldn't have to write a custom event just to handle a right click.

    If I'm not missing anything, and it's not implemented yet, I can pick up events from the input system instead. I just need to be able to determine what UI element is under the cursor to determine what action to take. I've noticed the UI events use a different coordinate system to the game, so I'd need to somehow convert from game pixel position to full screen pixel position...
    Any recommendations for how to go about that?
     
  2. threedots1

    threedots1

    Joined:
    Oct 9, 2014
    Posts:
    88
    So I've managed to register for PointerUpEvent instead, which lets me determine whether it's a left or right click and proceed from there. Works as expected.

    However I've tried registering for a PointerDownEvent and it never fires, so I can't set the current VisualElement under the cursor and check if it has changed when PointerUpEvent is fired...
     
  3. sebastiend-unity

    sebastiend-unity

    Unity Technologies

    Joined:
    Nov 9, 2015
    Posts:
    184
    If your PointerUpEvent handler is called properly, it means mouse events are working properly in your (runtime) environment. So we can eliminate that. Is it possible another PointerDownEvent handler or a mouse manipulator that consumes the event before your own PointerDownEvent handler? I would look for that first.

    Sebastien
     
  4. threedots1

    threedots1

    Joined:
    Oct 9, 2014
    Posts:
    88
    I couldn't get pointer down events working at all. I don't have anything in my code consuming the event as far as I can see, these are the only pointer events I'm registering.

    I've switched to using MouseDownEvent instead and it works fine.
     
  5. ImmanuelScholz

    ImmanuelScholz

    Joined:
    Aug 10, 2017
    Posts:
    4
    3 years later... ContextClickEvent is still broken ingame.

    I guess the Lessons Learned is rather: Don't use an UI Toolkit for ingame that was made for unity editor UI tooling?