I am trying to under how "Raycast Target" option works. I think the it works is
- if this is checked, then my UI element will consume the mouse/touch events and any 3D object behind it will not get the mouse/touch event.
- If Unchecked, this will pass the mouse event to 3D object behind it.
but his is not how it is behaving.
I have a UI text on canvas and a 3D cube gameobject behind it.I have added following script on UI text
public class CaptureUIObjectEvent : MonoBehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("In UI click event");
}
}
I have added following script on 3D object
public class Capture3DObjectEvent : MonoBehaviour
{
void OnMouseDown()
{
Debug.Log("In 3D click event");
}
}
When "Raycast Target is checked" : Both the UI text and 3D object is capturing the event. So both the logs are printed. The UI text is not blocking the click event. I think this is not the correct behavior. Also the 3D object event log is printed first. I think Unity has multiple default raycaster in scene for UI (graphics raycaster) and 3D (Physics raycaster) objects. Is it so? Is this behavior expected then and does this mean my 3D object will get the mouse click always?
When "Raycast Target is Unchecked" : Only 3D object is getting clicked. This is expected I think.
I have to make the CaptureUIObjectEvent derive from IPointerClickHandler to make it capture any event but for 3D object I dont have to use that. I thibk this is also because of different raycaster used by UI and 3D objects. Is it so?