- Home /
Allow Click Through some UI Elements
Hello, I am using IsPointerOverGameObject() method to prevent clicks through UI elements. It's working perfectly but I want to click through some UI elements with different tags. How can I do it? Thank you.
Answer by xibanya · Oct 09, 2020 at 04:37 AM
If you want some UI elements to basically be "invisible" to clicks, there are a few options
set "raycast target" to false on individual elements (images, text)
make the elements you don't want to recognize clicks children of a CanvasGroup with "interactable" and "blocksRaycasts" set to false
to actually use tags as you mention, you'll want to have a special script attached to the canvas that holds these objects. Make sure that canvas has a GraphicsRaycaster component and then in Start use GetComponent to assign that to a variable like
graphicsRaycaster
. Also have aprivate PointerEventData pointerEventData;
Then for your IsPointerOverGameObject, have something likebool IsPointerOverGameObject() { pointerEventData.position = Input.mousePosition; graphicsRaycaster.Raycast(pointerEventData, out List<RaycastResult> results); foreach (RaycastResult result in results) { if (result.gameObject.tag != "ignore") { //whatever other evaluation logic return true; } } return false; }
Your answer
Follow this Question
Related Questions
IsPointOverGameObject() not working as intended!!! 2 Answers
understand how "Raycast Target" works on UI elements 2 Answers
Different between IPointerClickHandler.OnPointerClick and MonoBehaviour.OnMouseDown 1 Answer
Holding down UI Button and shooting raycast from touch position doesn't work simultaneously:(( 0 Answers
Collider Blocking Worldspace Canvas? 1 Answer