Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Have UI Consume Mouse Click

Discussion in 'Input System' started by Snackmix, Mar 4, 2020.

  1. Snackmix

    Snackmix

    Joined:
    Apr 3, 2013
    Posts:
    36
    I have a canvas button that calls a function when clicked. However, elsewhere in my code I was using

    Mouse.current.leftButton.wasPressedThisFrame to do a check for another process in game. Currently when I click on the UI button both the button.OnClick() and my function checking for the mouse click will fire.

    I would like to have the button(or any UI clicked on) consume the action so that my other code doesn't run at the same time. Does anyone know how to set this up correctly with the new input system?

    I can post actual code if that will help, but currently on my phone.

    Thanks!
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    ATM there's no built-in support for having actions consume input such that they prevent input elsewhere. This is on the high-priority list for after 1.0.
     
    Maeslezo likes this.
  3. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    I have the same problem! I have on screen button (which is very common things to do?) for menu etc, but it also causes the onclick event to be fired for the player action so now my camera or player moves to that spot because that is the game play.

    I just can't believe that version 1.0 is out without addressing such basic issue. This is just the most basic and fundamental stuff that is needed to be addressed? How do you expect us to use such input system without it? What is the work around?
     
  4. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    325
    I have been checking the last release, 1.1.0-Preview 2, from October 2020 and I didn't find anything about event consuming.

    Is it still in the roadmap?

    Thank you
     
    Rudow likes this.
  5. Rudow

    Rudow

    Joined:
    Nov 26, 2020
    Posts:
    2
    Any update on this?
     
  6. ChristmasEve

    ChristmasEve

    Joined:
    Apr 21, 2015
    Posts:
    45
    Still no update? I guess we have to do it manually. It's easy if it's all in one script. You can simply set a bool to true and check it from other places that try to use a click event. When you have tons of separate scripts like you usually do in a Unity project, I don't know the cleanest way to circumvent this Unity shortcoming other than have a GameObject in your scene with a public property you can set to false at the beginning of each frame and set to true once it is consumed. Any scripts that need a left/right click must check that property first (or pair of properties if you wish to eat left and right clicks separately).
     
  7. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,225
    we're at 1.1, where are you at with that?
     
  8. ChristmasEve

    ChristmasEve

    Joined:
    Apr 21, 2015
    Posts:
    45
    I just realized left and right click being separate doesn't make sense... not in the same frame. How can someone use both in one frame? haha

    But, what did you mean by 1.1? Unity version? I'm on 2020.2.1f1. Were you saying the 1f1 part? Not sure if you're using 2020 or the latest LTR version but you can't eat events on 2020.2.1f1.
     
  9. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,225
    Oops I quoted the wrong message.
     
  10. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,225
    We're on 1.1 how is that high priority doing?
     
    Slight0 likes this.
  11. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    943
    Has this been implemented in the meantime?
     
  12. ChrisBlackwell

    ChrisBlackwell

    Joined:
    Jul 10, 2019
    Posts:
    2
    Wish I'd seen this before I switched to the new UI :( It's hard to imagine this isn't priority for 1.0, it makes a mouse controlled game a pain if you have a mouse controlled game and UI on the screen.

    I've solved it by creating empty objects over clickable UI regions that I check in my gameplay code but it's really janky and brittle.
     
    Last edited: Mar 12, 2021
  13. Paprik

    Paprik

    Joined:
    May 25, 2014
    Posts:
    14
    Can we please get an update? This is the number 1 thing that every mouse controlled game needs (and that is a lot of games).
     
    Kylotan and Maeslezo like this.
  14. TOES

    TOES

    Joined:
    Jun 23, 2017
    Posts:
    134
  15. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    142
    Just leaving this here for other people who stumble upon this and like me got the impression that some manual raycast-based / invisible colliders "hack" is necessary or found that
    EventSystem.current.IsPointerOverGameObject()
    from the previous post returns true for UI and non-UI objects alike:
    The EventTrigger component in combination with a PhysicsRaycaster on the camera can be used to react to clicks on scene objects without manual ray-casting and doesn't get triggered when a RaycastTarget UI element "blocks" clickable scene objects.
    So far this seems to work for me, still need to double-check if it works with touch input as well though.