Unity UI - Blocking clicks

Kiwasi Games · 114K views · 2014 · 4:58

Open on YouTube

Game programming & enginesIndie game developmenttutorialdated

Why Game Development Stack Exchange recommends it

Game Development Stack Exchange answerers point people to it when clicks on a Unity UI panel or canvas pass through to the scene underneath, or when they need to use the EventSystem for click handling.

Recommended by 4 people across 3 different months, 2015 to 2018.

Why Stack Overflow recommends it

Stack Overflow users link it when answering how to stop UI clicks or touches from passing through to game objects in Unity, since the official documentation on this is thin.

Recommended by 3 people across 3 different months, 2015 to 2016.

What it is

A short Unity tutorial by Kiwasi Games that shows three ways to stop clicks on UI elements from also hitting objects in the game world. It runs from the simplest approach (checking whether the pointer is over a UI object) to using Event Triggers and the EventSystem interfaces like IPointerClickHandler.

What people said

Recommends Muhammad Faizan Khan · 2015-08 · Stack Overflow · on “Detect mouse clicked on GUI”

Finally got my answer here [LINK] : There are three ways to do this, as demonstrated in this video tutorial [this video] . this video save me:). Use EventSystem.current.IsPointerOverGameObject Convert your OnMouseXXX and Raycasts to an EventSystem trigger. Use a physics raycaster on the camera Implement the various handler interfaces from the EventSystems namespace. Use a physics raycaster on the camera.

Recommends Ageonix · 2016-02 · Stack Overflow · on “What is the most elegant way to let UI objects swallow touches in unity 5?”

You can just use a 2d or 3d physics raycaster on the camera to accomplish this. BoredMormon has a great 5-min video on how to do this, I'd recommend checking it out! [this video]

Recommends Programmer · 2016-04 · Stack Overflow · on “how to prevent others than the top most gameobject receive clicked event in a stacked gameobjects”

Include using UnityEngine.EventSystems; In your OnMouseDown() function, add if (!EventSystem.current.IsPointerOverGameObject()) then put all the code that is suppose to run when mouse is down inside the if statement. using UnityEngine.EventSystems; public void OnMouseDown() { if (!EventSystem.current.IsPointerOverGameObject()) { //Put all your code for your gameobject here } } That should work but if it doesn't, Watch this [this video] video for other ways to do this.

Recommends Allen · 2016-05 · Game Development Stack Exchange · on “Physics2D raycasting with a perspective camera and tilted sprites”

Have you considered using the EventSystem [LINK] instead of raycasting yourself? In a nutshell: Add a "Physics2DRaycaster" componenent to your camera Add a 2D collider to your GameObject Create a class that implements UnityEngine.EventSystems.IPointerClickHandler [LINK] , and place it on your object with the collider. You will find that the EventSystem can be enormously useful, it has a decent array of interfaces beyond the one listed above. Check out this great little video for more if you are interested [this video] , it contains some examples of how to use the EventSystem for various things like making sure your clicking on an object and not part of the UI, how to replace OnMouseDown, etc.

Recommends Candid Moon _Max_ · 2016-12 · Game Development Stack Exchange · on “Prevent Unity click from going through Canvas”

Check if you have **Even System** in your hierarchy. `!EventSystem.current.IsPointerOverGameObject()` should work just fine with it. Check out this video if it still doesn't work UI Block [this video]

Recommends Aditya Yadav · 2016-12 · Game Development Stack Exchange · on “Disable a Prefab when Clicked”

I created a new gameObject and added this script: Why did you created a new gameObject when you wanted to disable the prefab clones?. There's nothing wrong with the given code. You should put the script on your Prefab . If it still doesn't work. You can use EventSystems [LINK] to get the Game Object that is currently selected and deactivate it. Check this link [this video] for how to use Event Systems.

Recommends reggie_7 · 2018-02 · Game Development Stack Exchange · on “Unity 5.. How to Block interaction below panel?”

I had a similar issue and I found this video [this video] to be very helpful, all credit to kiwasi.com [LINK] . It's showing 3 possible solution approaches, from the simplest to most complex (and least to most recommended): Use EventSystem.current.isPointerOverGameObject() from UnityEngine.EventSystems just as MrDiVolo [LINK] explains Event Triggers [LINK] (check also here [LINK] ) Event System Interfaces ( IPointerClickHandler [LINK] etc.)

Cites Cobertos · 2015-02 · Game Development Stack Exchange · on “How does the Graphic Raycaster component work in Unity?”

What is a raycast? What are blocking objects and blocking mask? A ray is a mathematical device that starts at an origin point and continues on in a specific direction forever. With a raycast you're casting a ray, cast being used like the word throw. It's like if you threw a rock and it continued on in that direction forever, it wouldn't stop until it hit something. You're interested as to whether it hit an object and what that object was. Blocking objects would be the objects that you specifically define your raycast to be able to hit. A blocking mask allows you to, instead of passing tons of blocking objects, define the layers in Unity you want your raycast to be able to hit in the form of

Cites VonnCC · 2015-09 · Stack Overflow · on “Unity touch event on overlayed objects”

found the answer here, Unity docs is really hard to understand especially for beginners, unlike CoronaSDK docs it's very user friendly here is the link on how to do this: Credit to guy in here [this video]

Cites Rufus · 2016-08 · Stack Overflow · on “How to handle full screen touch interaction with UI elements”

Suppose I have a game where the whole screen is interactable aside from a few UI buttons / elements (i.e. the screen-wide touch handler should be called when touches occur anywhere outside the UI buttons, but not be called when the touch occurs on the UI buttons, since touches occuring on the UI bottons should call the button handler, not the screen-wide touch handler) What is the best way to block the touch events when the touch occurs on the UI elements? I've seen multiple approaches... Attach the touch script to an arbitrary gameobject. In the Update() function, check EventSystem.current.IsPointerOverGameObject(touch.fingerId) and ignore if it is not false This seems to be most straight f