I am looking to detect if my cursor is on top of any UI elements (GameObject). I want to prevent running a function by checking if my cursor is on top of any UI Element.
The function looks like this:
if (!pointerIsOnTopOfEvent) {
return;
} **// if the pointer here is on top of any UI element, stop doing the rest of the code**
if (build_Manager.GetFarmObjectToBuild()== null)
{
return;
}
nodeRender.material.color = hoverColor;
I really appreciate it if someone has other methods if EventSystems.EventSystem.IsPointerOverGameObject is deprecated.
Try this
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;
}
I have discovered that it is NOT deprecated, or at least not completely. In the documentation, you’ll notice UnityEngine.EventSystems is missing after 2019.1. Why this is, i don’t know.
However, you can still get at IsPointerOverGameObject
by way of EventSystem.current.IsPointerOverGameObject()
, and make sure you are using UnityEngine.EventSystem
.
me too, i need another solution to detect click on UI
If you don’t mind the new Input System it has an isPointerOverGameObject method.