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.
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.
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?
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).
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.
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.
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.