- Home /
Question by
fruitaleman · Jun 25, 2014 at 09:43 AM ·
uiguibuttonphysics2dcollider2d
Detecting top most UI element using Collider2D
Hi guys,
I am using Physics2D and Collider2D to detect button clicks in my UI. It works well, only problem is if there is a UI overlap (a part of one button overlaps with a part of another button). I only want the top most UI, i've used Sorting Layer in the Sprite Renderer to show which UI is in front, but Physics2D.OverlapPoint still detects the lower button sometimes.
The following code exists for all my buttons:
if (Input.GetMouseButtonDown(0)) {
Vector3 wp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 touchPos = new Vector2(wp.x, wp.y);
if (collider2D == Physics2D.OverlapPoint (touchPos)) {
// Do button click event here
}
}
Comment