Tracking the mouse off-window

I would like to know whether or not it’s possible to have the Standalone be able to continue tracking the mouse even after it leaves the window, because I’m having issues where the application I’m building is still noticing Mouse Scrollwheel axis movement and acting accordingly.

The Editor seems to take care of this - it continues tracking the mouse position even if the Editor doesn’t have Windows’s focus. Is there any way I can put this on the finished build as well?

I managed to come up with a workaround until the people at Unity Technologies let us get the mouse position. A MouseBlocker GameObject with a few simple components:

  • An Image with no sprite, and a transparent tint (aka alpha = 0)

  • A CanvasGroup

  • A MonoBehaviour that goes like:

    public Camera referenceCamera;
    private CanvasGroup group;
    public Vector2 lastMousePos;
    public Vector2 mouseDelta;

    public static bool BlockMouseInput = false;

    void Update() {
    Vector2 currMousePos = Input.mousePosition;

     Vector3 newpos;
     RectTransformUtility.ScreenPointToWorldPointInRectangle(((RectTransform)this.transform), currMousePos, referenceCamera, out newpos);
     transform.position = newpos;
    
     mouseDelta += new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
     if((Vector3)lastMousePos != Input.mousePosition) {
         mouseDelta = Vector2.zero;
    
         lastMousePos = Input.mousePosition;
     }
    
     if(group == null) group = GetComponent<CanvasGroup>();
     group.blocksRaycasts = BlockMouseInput = (mouseDelta.sqrMagnitude > 0.15f);
    

    }

It takes care if pretty much everything under the uGUI with the CanvasGroup blocking the Raycasting, but anything referencing Input.GetAxis on any of the mouse axes should check BlockMouseInput first.