I receive input from the mouse with the New Input System, but I do not want to receive input while the mouse is on any UI object, regardless of it being over or under any non-UI object. I tried EventSystem.current.IsPointerOverGameObject(). It seems to work, but it also gives a warning every time input is received.
Calling IsPointerOverGameObject() from within event processing (such as from InputAction callbacks) will not work as expected; it will query UI state from the last frame UnityEngine.EventSystems.EventSystem:IsPointerOverGameObject () ...
Here is my code:
private void OnEnable()
{
mouseClick.Enable();
mouseClick.performed += MousePressed;
}
private void OnDisable()
{
mouseClick.performed -= MousePressed;
mouseClick.Disable();
}
private void MousePressed(InputAction.CallbackContext context)
{
if (EventSystem.current.IsPointerOverGameObject()) return;
Ray ray = _playerCam.ScreenPointToRay(Mouse.current.position.ReadValue());
RaycastHit hit;
...
}
What should I do?
If you want to check whether you've clicked on any
UIobject, you can implement it usingEventSystem.First we get the
PointerEventDatafrom thecurrentEventSystemand assign itsPointerEventData.positiontoInput.mousePosition. We don't have to convert it, as thepositiontakes coordinates inscreen space, which is also the space of the returned value. This can be also implemented like this. Whatever feels more readable for you.Then we call the
EventSystem.current.RaycastAllmethod for previously setPointerEventDataand store theList<RaycastResult>in our createdresults. This method gets everyUI Objectwhich hasGraphic.raycastTargetset totrue. Otherwise those objects don't even respond to user's clicks.If the list isn't empty, we break the method, because there's at least one
UIfound. Now, if you want to check thisUIbeing e.g. aButton, you've mentioned in comments, you can enumerate through its items and check for it.Additionally, you can find your
UIby a tag.Notice that
CallbackContext's name is_, as we don't need it. In your case its value isfloat, which returns1if pressed and0if not.