- Home /
Raycast target on UI elements
What is the purpose of the "Raycast Target" flag on many UI elements, like images and text, and how does this impact on overall performance?
Answer by salex100m · Nov 17, 2015 at 03:49 AM
Quite simply, if selected (check box is checked), the UI element will block the raycast (i.e. your mouse click). If you don't have it checked, then your mouse will not work on that button, and the next thing below/behind it will catch the raycast.
The point is to remove the mouse-clickability from UI elements (like maybe a name tag) that don't need to be clickable (raycast target=false).
Likewise, if you have a UI element covering half the screen (like a window or something), you probably don't want to click things behind it so (raycast target=true)
No performance impact either way.
Actually, there is a performance impact. Every element of the UI that has "Raycast Target" sett will be a part of Raycast call results, thus adding allocated result structure and CPU time necessary to process it. So, if you have a sophisticated UI with many elements in it, this can add to quite a lot. The rule of thumb - don't need interaction, turn off 'raycast target'.
Have you measured the actual performance impact? At what point does a UI become sufficiently complex that micro-managing the raycast flag is worthwhile?
If you want to follow best practices of game development, the answer is "at any point".
"If you don't have it checked, then your mouse will not work on that button" It doesn't seem to be the case, I'm deactivating some raycast target of some images with buttons at runtime and the buttons are still receiving button clicks, does someone has a clue why?
That's because Button uses 2 raycast targets :
One is in its Image Component with the usual "Raycast Target" checkbox. Turning it off won't turn off the button effect when pressed, but will allow the click to pass through the button even if it's activated.
The other one is in the Button Component which is the "Interactable" checkbox. The Button component doesn't block the raycast, but recognize when it's "hit" by it. (A bit like a "OnCollisionEnter" collision trigger).
So if you only turn off the image Raycast, the button Component will still work, but if another Interactable element is behind that button, it will also be activated. If you only turn off the "Interactable" in the Button Component, the Raycast will be blocked by the button, but it won't do anything (and will be displayed as it's Disabled state).
I also had to uncheck "Raycast Target" on the Text (inside the Button) to be able to interact with another UI element, placed behind.
@fadden ... As games today get bigger and bigger, with layer upon layer of complexity, one cannot argue that optimization needs to be done to everything at every level. Granted, the impact of a few extra RaycastTargets is going to be $$anonymous$$imal in a simple UI but this, compounded with other unnecessary processes, all adds up in the end to a slower game.
For any late comers to this thread ... $$anonymous$$y take is use it where needed! I have had to fix issues with other's UIs that weren't working because the developer had layered text and an image on top of their UIs buttons. Because the text and/or image was blocking the button never received the on-click events.
Answer by the_real_ijed · Jan 02, 2018 at 04:32 PM
Having to turn off this default on checkbox every time you add new UI elements is just annoying busywork - it'd be great if this was fixed engine side.
Answer by andrew_pearce · Apr 13, 2020 at 09:59 AM
It's worth to mention that if element has Raycast Target property disabled but ANY of its child element has it enabled, the parent will still get IPointerClickHandler event!
It's a bit old post, but do you have any reference for this? I need some excuse not to be bothered unchecking all. I just tested 2019.4.15 unity and it seems it does call all the IPointerClickHandler event of parents, but it does not check raycast intersection for the parents with raycast target property turned off.
Your answer
Follow this Question
Related Questions
How to get UI element into HMD Unity 5.3.4p1 - Oculus Utilities 1.3 0 Answers
Create UI raycast like a mouse Click 2 Answers
Raycast a World Space UI with VR (Daydream) 1 Answer
How do I have clickable buttons in a scroll rect? 1 Answer
Is it possible to configure raycast to hit opaque pixels without using alphaHitTestMinimumThreshold? 1 Answer