How to prevent a colliders OnMouseDown() from being called if it is underneath a UI panel?

Hello Unity Community,

how do you prevent a colliders OnMouseDown() from being called if the collider (or it’s GameObject to be more precise) is underneath a UI panel?
In other words: I’d like to cancel the mouse event, if the UI panel catches it. Can this somehow be done?

If the UI would block raycasts then you would not have to ‘cancel’ any input. You should always prevent things from happening rather than cancelling them after they’ve happened. Because then you’d have to do extra tests to see if it should cancel or not.

“Discretion is the better part of valor”