Virtual Mouse and Canvas Scaler Scale with Screen Size

The Gamepad Mouse Cursor sample doesn’t seem to be compatible with the Canvas Scaler “Scale With Screen Size” setting. If the Canvas Scaler scales the canvas, the virtual mouse cursor creates pointer events at a location different to where it’s rendered.

Did anyone experience this aswell? Any solutions?
Thanks!

4 Likes

Yes! I have the exact same problem… Did you manage to solve it?

Using Hardware Cursor does work as expected. But If I use Software Cursor it seems to be off.

I’m testing with 1280x720 resolution using Canvas Scaler “Scale With Screen Size” - Height =1

There is a Pull Request to fix This from more than 1 year ago!

FIX: VirtualMouseInput not supporting UI scaling other than constant. by Rene-Damm · Pull Request #1119 · Unity-Technologies/InputSystem (github.com)

I really don’t understand why it hasn’t been updated with this and other pull requests that have already been submitted.

Well… Nothing works… The fix also seems to does not work… And If I update to the preview versions the GamePad Mouse Cursor Sample then does not work at all.

Has anybody found a solution to this a year later?
I have the same issue with latest version 1.4.2

In new Input System, the pointer events are always offset when canvas is set to “Scale With Screen Size”.

I think have found a solution! If you copy all of the VirtualMouseInput.cs code into your own script, then make the following changes (I am using Input System v1.5.0)

  1. Add a reference to the Canvas RectTransform component “canvasRectTransform” in the code below.

  2. Add this function

// Clamp the virtual mouse position to the canvas
    private Vector2 ClampToCanvas(Vector2 position)
    {
        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, position,
            m_Canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : m_Canvas.worldCamera, out position);

        return position;
    }
  1. Replace this on line 453
m_CursorTransform.anchoredPosition = newPosition;

with this

m_CursorTransform.anchoredPosition = ClampToCanvas(newPosition);

This should work for overlay canvas’s and camera space canvas’s, and I am currently testing whether it works with world space ones. Let me know if it works for you!

4 Likes

Thank you, with one simple tweak this has done the job for me!!

At first I had a null reference exception because m_Canvas was null.
Adding the following if statement at the beginning of UpdateMotion fixed it.

if (m_Canvas == null)
TryFindCanvas();

And also don’t forget that your virtual cursor should have the anchored preset set to center in order to work properly.

3 Likes

Unfortunately for me this fix doesn’t appear to work

Edit: Nevermind, I was doing it wrong… I cannot even explain how happy I am that this is working now. This has been such a headache for 2 days hah