Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Virtual Mouse and Canvas Scaler Scale with Screen Size

Discussion in 'Input System' started by smonbrogg, Jun 20, 2021.

  1. smonbrogg

    smonbrogg

    Joined:
    Dec 11, 2012
    Posts:
    17
    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!
     
    ATate and Radivarig like this.
  2. wagenheimer

    wagenheimer

    Joined:
    Jun 1, 2018
    Posts:
    322
    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
     
  3. wagenheimer

    wagenheimer

    Joined:
    Jun 1, 2018
    Posts:
    322
  4. wagenheimer

    wagenheimer

    Joined:
    Jun 1, 2018
    Posts:
    322
    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.
     
  5. ATate

    ATate

    Joined:
    Sep 13, 2018
    Posts:
    6
    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".
     
  6. KineticBen

    KineticBen

    Joined:
    Jul 19, 2021
    Posts:
    10
    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
    Code (CSharp):
    1. // Clamp the virtual mouse position to the canvas
    2.     private Vector2 ClampToCanvas(Vector2 position)
    3.     {
    4.         RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, position,
    5.             m_Canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : m_Canvas.worldCamera, out position);
    6.  
    7.         return position;
    8.     }
    3. Replace this on line 453

    Code (CSharp):
    1. m_CursorTransform.anchoredPosition = newPosition;
    with this

    Code (CSharp):
    1. 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!
     
    Last edited: Apr 25, 2023
    r35 and PauloxWg like this.
  7. PauloxWg

    PauloxWg

    Joined:
    Aug 1, 2020
    Posts:
    4
    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.
     
    r35 likes this.