unity-game-engine

Warning when calling IsPointerOverGameObject after updating Unity; is there an alternative?


I have an InputAction callback where I am recording the position where the player clicks the screen, but only if the click is not over a UI element. Here's my code

private void OnPress(InputAction.CallbackContext context)
{
    if (!EventSystem.current.IsPointerOverGameObject())
    {
        this.pressPosition = Mouse.current.position.ReadValue();
    }
}

This has been working correctly. However, I recently updated my version of Unity, and now I'm getting this warning every time I click somewhere in my game:

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

According to the changelog, this warning was added with an update to the input system.

Is there a way to figure out whether the mouse was over the UI when the player clicks the screen without getting this warning?


Solution

  • how I solved it was by moving just that piece of logic to an Unity's Update function:

    private void Update()
    {
      if (Mouse.current.leftButton.wasPressedThisFrame)
      {
        if (EventSystem.current.IsPointerOverGameObject(PointerInputModule.kMouseLeftId))
          // was pressed on GUI
        else
          // was pressed outside GUI
      }
    }
    

    You can still keep using the inputsystem, i.e. when cancelled:

    private void OnPress(InputAction.CallbackContext context)
    {
      if (context.canceled)
        // Left mouse is no longer pressed. You can do something within the input system notification.
    }