- Home /
Detect only UI button click
In HUD panel of game, I have set Pause game button. Behind this, there is big collider object in game play which is also clickable.
So at present, when I click on Pause button, both event get executed. Pause button click and Collider Raycast code. I want to implement like, at area of Pause button only that button action get executed not Collider Raycast.
Please give me some help into this.
Answer by siddharth3322 · Oct 20, 2016 at 01:56 PM
Totally credit goes to @fabian.mkv and for this post
This working like a charm :)
IsPointerOverGameObject not working with touch input
private bool IsPointerOverUIObject() {
PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
return results.Count > 0;
}
Answer by Zodiarc · Oct 12, 2016 at 02:56 PM
I think somwhere in the canvas or the button itself should be a checkbox saying "Block raycast" or something similar
Answer by Efril · Oct 12, 2016 at 03:14 PM
if(!EventSystem.current.IsPointerOverGameObject())
{
Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit raycastHit;
if (Physics.Raycast(cameraRay , out raycastHit))
{
//Do something with hit collider.
}
}
EventSystem.current.IsPointerOverGameObject() is the way to separate clicks on UI game object and clicks on 'in scene' game objects.
Thanks for your quick reply but this thing is not working on Android platform. On Unity Editor its working correctly...