There is a simple scene with some gameObjects. They all have a Box Collider and an OnMouseDown() method.
I would like to add a UI overlay that would block the gameObjects from receiving mouse events. It seems that the standard way to do that is to add Canvas with render mode “Screen Space – Overlay” with an image that is a Raycast Target.
I tried that but that doesn’t work. Also checked Google and other resources, but couldn’t find an answer that works. This is strange, given that many games would have a similar situation when displaying a modal dialog.
OnMouseDown
will always respond to the mouse down event. Canvas elements will not prevent this message from being sent to the object.
Here are the steps to resolve the issue.
IPointer
interfaces in your MonoBehaviour.public class MyObjectBehaviour : MonoBehaviour, IPointerDownHandler
{
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log($"OnPointerDown");
}
}
Now your object will respond to the pointer down event only when it is not blocked.