Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Input.mousePosition buggy in resizable window (windows exe)

Discussion in 'Editor & General Support' started by andyz, Jan 21, 2015.

  1. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,251
    So was trying some unity code with mouse input exported as a windows exe with "resizable window" ticked and there is a bug with the coordinates returned after resizing the window (upwards?) sometimes - when you click in the window beyond where the previously-sized window was the coordinates get 'cropped' to the old smaller size.

    (i.e. Mouse Coordinates sometimes wrongly reported after window re-sized!)

    Any known issue here?

    It is not easy to replicate consistently but is a pain when it occurs as mouse clicks throughout certain areas of the window start returning the same x or y coordinate cropped to a min value - Windows 7, latest Unity 4.6.1
     
    Last edited: Jan 23, 2015
  2. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,251
  3. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,251
  4. Dm1try

    Dm1try

    Joined:
    Jul 25, 2011
    Posts:
    8
    I wrote this code that really fixes this bug. Of course you should temporarily use it until Unity3d-team fix the bug.

    When resolution is changed this code calls function SetResolution(). It makes update internal parameter like screen size for Input.mousePosition. Afther that it works perfectly.

    You should create an object of ScreenGuard and call Update() every frame (or you can convert this class to Monobehaviour).
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. /// <summary>
    4. /// Should use temporarily until Unity3d-team fix the bug
    5. /// http://issuetracker.unity3d.com/issues/input-when-resizing-window-input-dot-mouseposition-will-be-clamped-by-the-original-window-size
    6. /// </summary>
    7. class ScreenGuard
    8. {
    9.     private int prevWidth;
    10.     private int prevHeight;
    11.  
    12.     private bool isInited = false;
    13.  
    14.  
    15.     private void Init()
    16.     {
    17.         prevWidth  = Screen.width;
    18.         prevHeight = Screen.height;
    19.  
    20.         isInited = true;
    21.     }
    22.  
    23.     private void SetResolution()
    24.     {
    25.         prevWidth  = Screen.width;
    26.         prevHeight = Screen.height;
    27.  
    28.         Screen.SetResolution(Screen.width, Screen.height, Screen.fullScreen);
    29.     }
    30.  
    31.     public void Update()
    32.     {
    33.         if (!isInited)
    34.         {
    35.             Init();
    36.         }
    37.  
    38.         if ((Screen.width != prevWidth) || (Screen.height != prevHeight))
    39.         {
    40.             SetResolution();
    41.         }
    42.     }
    43. }
     
  5. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Thanks, Dm1try.
     
  6. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Finally, it's been fixed in the current beta.

     
  7. Kaz_Yamof

    Kaz_Yamof

    Joined:
    Jan 17, 2013
    Posts:
    28
    I'm still having this issue here (5.0.1.f1)
    When I build the scene and maximize the window, the UI stop responding.
     
  8. CoDeXiTo

    CoDeXiTo

    Joined:
    May 11, 2013
    Posts:
    10
    i have this problem on unity 5.1.2f1 :(
     
  9. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    My old repro works fine in 5.1.2f1.