Input
The common way to interact with the interface is through clicks, but the same interaction with other objects occurs through clicks.
And most often problem is when the same click is processed by UI and another object simultaneously.
There are a couple of solutions to this problem.
If you are using the OnMouseX()
method then you should use EventSystem.current.IsPointerOverGameObject()
to check if a click should be processed.
protected void OnMouseDown()
{
if (EventSystem.current.IsPointerOverGameObject())
{
Debug.Log(Time.frameCount + " ignore click because over UI");
return;
}
Debug.Log(Time.frameCount + " OnMouseDown");
}
But a better way is to implement the IPointerClickHandler
interface.
In this case, you do not need additional checks, but you should add the PhysicsRaycaster
component to the camera.
public class Test3DClick : MonoBehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log(Time.frameCount + " OnPointerClick");
}
}
The second method is recommended because it uses the same EventSystem as the UI objects.
MonoBehaviour Method |
Interface |
---|---|
OnMouseDown |
|
OnMouseDrag |
|
OnMouseEnter |
|
OnMouseExit |
|
OnMouseOver |
|
OnMouseUp |
|
OnMouseUpAsButton |