- Home /
Different between IPointerClickHandler.OnPointerClick and MonoBehaviour.OnMouseDown
Hello! I tried to refresh my memory on Unity's UI stuff and came across this question. In my scene, I have:
A GameObject with Collider and a script that implements OnMouseDown().
A UI Button that overlaps the GameObject as seen from the Camera.
When I pressed the button with the mouse also in the same position as the GameObject, both the button and the GameObject register the click.
After that, I made the GameObject implement OnPointerClick() instead (with Raycaster on the Camera) and the Button seems to block the click just fine.
I believed that UI should always block the click from going through. Is this the difference between both functions?
Answer by ryanmillerca · Oct 14, 2021 at 02:44 PM
OnPointerClick uses Unity's event system, OnMouseDown uses the physics engine. Therefore, OnMouseDown is generally better suited for gameplay controls than for UI. OnPointerClick should work best with the UI since it relies on EventSystem and should help you with clicking through objects etc. You won't need a Collider for OnPointerClick, but you will for OnMouseDown. This isn't to say that you can't use the EventSystem for gameplay input, if you wanted! I generally avoid OnMouseDown as it's a bit of a beginner technique and not very robust. You can implement your own raycasting code for more control and optimization, if you're comfortable doing so.