- Home /
4.6 UI "image" is capturing clicks - how to prevent?
I've got a world-space UI system featuring menus with buttons, and I've just tried putting a new UI element with an image component at the "draw last" point in the canvas hierarchy (bottom of hierarchy) to act as a cursor.
Because it's right beneath the mouse, its presence seems to capture 90% of clicks and other mouse events, preventing my buttons from receiving click events. Obviously that's bad, but I'm not seeing an obvious way to disable an image's ability to capture clicks. No toggles on the component itself, IntelliSense didn't show anything promising. Putting the cursor image on the IgnoreRaycast layer didn't help.
Anyone else encountered this and have a nice solution?
Answer by CanisLupus · Nov 16, 2014 at 04:08 PM
UPDATE: Currently the correct way to do this is to uncheck the "Raycast target" field on your Image/Raw Image/Text/etc component. :) See the other answers, namely the first one mentioning this, by @Carlotes247 (and upvote them!).
OLDER WAYS, FOR REFERENCE:
If you want to disable input on your UI element, a clean solution is to place this script on it:
using UnityEngine;
public class GraphicIgnoreRaycast : MonoBehaviour, ICanvasRaycastFilter
{
public bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
{
return false;
}
}
ICanvasRaycastFilter
lets you implement IsRaycastLocationValid
, which is called by Unity whenever your mouse/touch is over the element area. If we always return false
, it always says "nope, the cursor isn't over me" and the object will never block anything.
I hope this helps :)
Not related to your question: IsRaycastLocationValid
can also be used for circle buttons, where the mouse can be over the rectangular area of the element but not yet inside the button graphic (which would be a circle). In such a case you would only return true
if you calculated that screenPoint
was inside the radius of the button, for example.
You can also add the Canvas Group component to your image object and uncheck "Blocks Raycasts" (and possibly Interactable). This will make the object behave like it's not there, click-wise.
I recall finding a list of the UI event interfaces while investigating this - not sure how I missed that one! Big big thank-yous for providing this answer. :)
Hey Sunny, I'm aware this isn't the place, but I'm not sure how else to let you know, the black lines were caused by Unity grabbing a tiny bit of the tile above the grass, even though the editor said it didn't include any of the black. Can you report this information to Unity so that they now know how to fix it? It's not that it's not a known issue but they just don't know what causes it. I do now.
Answer by Carlotes247 · Dec 21, 2015 at 02:58 AM
Hello! There is a way now in Unity 5 (I'm working on 5.3, don't know if before) where you can uncheck one field on the image component called "Raycast Target". This way, you don't need to add a new component!
Answer by JennBren · Apr 15, 2018 at 04:01 PM
Greetings,
Just wanted to say, using Unity 2017.3 and by unclicking the "Raycast Target" box on the Image inspector still works great. There are a few updates of course to how it looks but this is still the correct method. Thank you so much for posting. :-)
Your answer
Follow this Question
Related Questions
UI - How can I prevent a Rect Transform on a canvas from blocking clicks? 1 Answer
4.6 UI OnBeginDrag event handler doesn't seem to work. 1 Answer
Raycast against UI in world space 4 Answers
How to wait click on button in my MessageBox analogue? 1 Answer
[4.6 - UI] How to call an event with button when the pointer is up the button image. 1 Answer