Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Screen.width is wrong in editor mode

Discussion in 'Editor & General Support' started by unisip, Jun 27, 2011.

  1. unisip

    unisip

    Joined:
    Sep 15, 2010
    Posts:
    332
    As you're releasing unity 3.4 soon, fixing the following issue would be greatly appreciated:

    Screen.width returns a wrong value when in editor mode.

    Expected result: Screen.width should give you the width of the game view. (same pb with Screen.height, of course)

    Tks
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It already does that...make sure you're using at least Unity 3.2, since 3.0 and 3.1 reported incorrect values for the width/height for the first few frames when using maximize on play. In any case, report bugs by using the bug reporter app that comes with Unity (make sure you're using the latest version); posting on the forums won't get anything fixed.

    --Eric
     
  3. unisip

    unisip

    Joined:
    Sep 15, 2010
    Posts:
    332
    Mmm, then I must be doing something wrong.
    I'm running Unity 3.3.0f4 (63135) on PC/windowsXP, I created and empty test project and just added the following editor to test Screen.width (C# code below)

    using UnityEngine;
    using UnityEditor;

    public class ScreenWidth : EditorWindow
    {
    [MenuItem("Test/ScreenWidth")]
    static void Init()
    {
    // Get existing open window or if none, make a new one:
    ScreenWidth window = (ScreenWidth)EditorWindow.GetWindow(typeof(ScreenWidth));
    }

    float width = 0;
    void OnGUI()
    {
    GUILayout.Label("screen width is:"+width);

    if (GUILayout.Button("refresh screen width"))
    width = Screen.width;
    }
    }

    When I test this with the editor, I always get Screen.width = 322, no matter how wide my game view is.
    Am I missing something?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Oh, an editor script...in that case, Screen.width apparently refers to the editor window width.

    --Eric
     
    Necronomicron likes this.
  5. unisip

    unisip

    Joined:
    Sep 15, 2010
    Posts:
    332
    Nope.
    Screen.width stay stuck on a fixed value that corresponds to neither of those views.
    In fact, I just realized Screen.width gives you the width of the editor view that the script is managing. This makes some kind of sense, although I don't see much use for it. Getting access to the game view size would be useful though.
     
  6. spinaljack

    spinaljack

    Joined:
    Mar 18, 2010
    Posts:
    984
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    By "editor window width" I was referring to the editor window that's created by the script. It's 322 pixels wide by default, and returns different values when you resize that window.

    --Eric
     
    Necronomicron likes this.
  8. unisip

    unisip

    Joined:
    Sep 15, 2010
    Posts:
    332
    ok, that's not optimal but at least that's a work-around
    - setting the app to webplayer
    - forcing the game view size to webplayer size
    - using the build target res in my editor script

    Another way would be to be able to access the game view editor window and retrieve its current size, but I found a way to do that either

    Last idea, but its a big big hack: creating and editor window that would read Screen.width and store it "somewhere accessible". Placing that editor window as a tab in the same space avec the game view ensures that this editor window has the same size as the game view (god, that' ugly :))
     
  9. nventimiglia

    nventimiglia

    Joined:
    Sep 20, 2011
    Posts:
    153
    Yeah my gui script uses Screen.width.

    At first the value is correct, but if I disable and then enable the script (thus causing a recalculation) the value is inaccurate.

    I am using Unity 3.41f2
     
  10. KalteWasser29

    KalteWasser29

    Joined:
    Jul 22, 2012
    Posts:
    1
    Is this still not fixed?!. How do we make something centered somewhere, depending on resolution then?
     
  11. unisip

    unisip

    Joined:
    Sep 15, 2010
    Posts:
    332
    As far as I oculd test, if you make a script that runs in editor mode and call Screen.width from OnGUI, you will get the right value when the game view has the focus (OnGUI is evaluated when the game view receives a mouse event).
    That way, you should be able to store the screen width value in order to reuse it elsewhere
     
  12. andrew-lukasik

    andrew-lukasik

    Joined:
    Jan 31, 2013
    Posts:
    239
  13. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  14. andrew-lukasik

    andrew-lukasik

    Joined:
    Jan 31, 2013
    Posts:
    239
    Are you trying to say there is a newer version? :eek:
     
  15. Estecka

    Estecka

    Joined:
    Oct 11, 2013
    Posts:
    62
    2017.4 here.
    Screen.height
    and
    Screen.width
    still returns the editor window's size, but instead you can use
    Screen.currenResolution
    .
     
    MUGIK, skabed, A_Marraff and 5 others like this.
  16. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,554
    2018.3.0b7 here and it is still like that..
     
  17. LucianoMacaDonati

    LucianoMacaDonati

    Joined:
    Dec 31, 2012
    Posts:
    10
    Using 2018.3.0f2 and Screen.currentResolution gives me the accurate value to calculate the camera Bounds. Screen.width gives me a smaller size that I have no clue where it comes from.
     
  18. Necronomicron

    Necronomicron

    Joined:
    Mar 4, 2015
    Posts:
    108
    It was already answered above, use this and this instead.
     
  19. Hullabu

    Hullabu

    Joined:
    Oct 23, 2014
    Posts:
    18
    This is not working by the way. In edit mode Camera.pixelWidth returns the same values as Screen.width and they both depends from selected widget in the editor.
     
  20. Hullabu

    Hullabu

    Joined:
    Oct 23, 2014
    Posts:
    18
  21. Necronomicron

    Necronomicron

    Joined:
    Mar 4, 2015
    Posts:
    108
    Why does it work for me then?
     
    Last edited: Jun 8, 2019
  22. Romeno

    Romeno

    Joined:
    Jun 24, 2013
    Posts:
    34
    Have written this basing on the answers and it appeared to work. Not sure it works in every scenario.

    Code (CSharp):
    1.     Resolution GetCurrentResolution()
    2.     {
    3. #if UNITY_EDITOR
    4.         return new Resolution
    5.         {
    6.             width = Camera.main.pixelWidth,
    7.             height = Camera.main.pixelHeight,
    8.             refreshRate = Screen.currentResolution.refreshRate,
    9.         };
    10. #else
    11.         return Screen.currentResolution;
    12. #endif
    13.     }
     
    Timmy-Hsu likes this.
  23. quizcanners

    quizcanners

    Joined:
    Feb 6, 2015
    Posts:
    108
    [UPDATE]
    Found THE SOLUTION: http://muzykov.com/unity-get-game-view-resolution/
    Works!


    To explain what is going on: when you are inside a custom Inspector script, Screen.width and Screen.height represent resolutions of the currently drawn window (where your Inspector/Editor is). Which caused me some confusion because I make lots of custom inspectors, put buttons there to do something, and that something sometimes dependent on the screen resolution.

    Screen.currentResolution - returns the resolution of my monitor which is always 1920 x 1080.

    Camera.main is a good solution but in some projects with multiple scenes, it may be a NULL.

    Currently looking for a solution, will update the post if I were to find a good one.

    This could be it:
    https://answers.unity.com/questions/560518/get-game-view-window-current-width-height-in-scrip.html
    Or this:
    https://forum.unity.com/threads/screen-height-reporting-editor-game-window-height.59571/
     

    Attached Files:

    Last edited: Jan 24, 2020
    Eristen and masterton like this.
  24. AlexTuduran

    AlexTuduran

    Joined:
    Nov 26, 2016
    Posts:
    27
    Screen.currentResolution just gives you the actual monitor resolution. 2019.2.19f1 and Screen.width / .height still returns the size of the inspector window, which is annoying, but apparently this works:

    Code (CSharp):
    1. string[] res = UnityStats.screenRes.Split('x');
    2. Debug.Log(int.Parse(res[0]) + " " + int.Parse(res[1]));
    Source: https://answers.unity.com/questions...45.1340150911.1586265652-147773489.1555672997
     
    wuhuan likes this.
  25. Imsoconstipated

    Imsoconstipated

    Joined:
    Nov 10, 2018
    Posts:
    4
    hey, this is weird but I get this problem. please try it out so that I don't sound crazy or tell me if I'm not making any sense. So I have this simple code to get the screen ratio.
    Code (CSharp):
    1.  float screanRatio;
    2.         if (Screen.width < Screen.height)
    3.         {
    4.             screanRatio = ((float)Screen.height / (float)Screen.width);
    5.         }
    6.         else
    7.         {
    8.             screanRatio = ((float)Screen.width / (float)Screen.height);
    9.         }
    that's not the weird part. the weird part is when I put that code on the Start() function it gives me the ratio of the game screen. But when I put it in the OnEnable() it gives me the ratio of the unity program. maybe this will answer some question and create more.
     
  26. Revolter

    Revolter

    Joined:
    Mar 15, 2014
    Posts:
    216
    Hey Unity, please give us a way to get the height and width of Game screen that would be consistent with the numbers you would get on a device.
    Some tools like SRDebugger allow you to enable cheats from an editor window at runtime, which would lead to weird behaviour if you're using Screen.width and Screen.height
     
  27. abcjjy

    abcjjy

    Joined:
    Mar 6, 2015
    Posts:
    35
    Screen.width, Screen.currentResolution, Camer.main.pixelWidth, non of them works for me. I'm using Unity 2109.4. Those properties all return some editor window dimensions when player window is not active.

    After experimenting for a while, I found a silly workaround. Add screen space canvas to the root of the scene and read its RectTransform.rect. This will give you actual game view size no matter which window is active.
     
    sonmium and Alpyne like this.
  28. msyoung2012

    msyoung2012

    Joined:
    Mar 20, 2014
    Posts:
    49
    Amazingly, still having this issue in 2021.2.14f1.
     
    nasos_333 likes this.
  29. Revolter

    Revolter

    Joined:
    Mar 15, 2014
    Posts:
    216
    From what I understood this is by design. Screen.width returns editor window width if getting from the editor thread/code
     
  30. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,009
    What might be important to mention is that

    Screen.currentResolution

    Is not working for me during play mode in game window but it does work when i switch to simulator, and properly grabs the resolution of different devices there.

    I'm using it inside "void Update()".
     
  31. ohavitd

    ohavitd

    Joined:
    Nov 1, 2021
    Posts:
    4
    The value 'Screen.width' and 'Screen.height' if not consistent. The VisualElements will wrong scaled in the Editor, but not all, on macOS with Retina display.

    I get the GameView directly, then if the 'width' and 'height' consistent.

    I manipulate the panelSettings on adding a UIDocument. This is not my favorite quick fix, but the view is near the appearance like the runtime.

    For example:

    Code (CSharp):
    1.  
    2. #if UNITY_EDITOR
    3.         private static EditorWindow GetGameView()
    4.         {
    5.             var assembly = typeof(EditorWindow).Assembly;
    6.             var type = assembly.GetType("UnityEditor.GameView");
    7.             return EditorWindow.GetWindow(type);
    8.         }
    9. #endif
    10.  
    11.         void AddUiDocument(xxxxx) {
    12.             // ...
    13. #if UNITY_EDITOR
    14.             var gameViewPosition = GetGameView().position;
    15.  
    16.             if ((int)gameViewPosition.width * 2 == Screen.width)
    17.                 IsRetina = true;
    18.  
    19.             if ((int)gameViewPosition.width * (IsRetina ? 2 : 1) != Screen.width)
    20.                 uiDocument.panelSettings.referenceDpi = Screen.dpi / (IsRetina ? 2 : 1);
    21.             else
    22.                 uiDocument.panelSettings.referenceDpi = Screen.dpi;
    23. #endif
    24.         }
    25.  
     
    masterton likes this.
  32. Kirsche

    Kirsche

    Joined:
    Apr 14, 2015
    Posts:
    120
    The issue tracker lists countless reports of this 11+ year old bug - all of which are closed with our favorite labels: "Not Reproducible", "Won't Fix" and "By Design".

    A true Unity classic.
     
  33. Meatloaf4

    Meatloaf4

    Joined:
    Jul 30, 2013
    Posts:
    182
    skabed and _geo__ like this.
  34. Eristen

    Eristen

    Joined:
    Jun 17, 2021
    Posts:
    15
    j1mmie likes this.
  35. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,903
    The worst part is that I have come across the Screen.width giving wrong number issue in build as well.