Search Unity

Get Screen Resolution (Not Window Resolution)

Discussion in 'Scripting' started by Studio_Akiba, Apr 17, 2015.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I am trying to get the screen size from Unity, but all I can set/get is the size of the window, I am looking for the size of the actual screen, not window, anyone know how to do this?
     
  2. Codermic

    Codermic

    Joined:
    Feb 12, 2015
    Posts:
    7
  3. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Screen.CurrentResolution
     
  4. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I am using this already, I am using this to specify the WINDOW size, I want to get the actual monitor size.
     
  5. Deleted User

    Deleted User

    Guest

    You mean the physical size of the screen or the native resolution?
     
  6. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    the physical size of the screen, in pixels, so for example 1920 x 1080 for a typical 16x9 monitor.
     
  7. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Pixels aren't a real world unit o_O
     
  8. Deleted User

    Deleted User

    Guest

  9. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    In the end I used ScreenPosition.width/.height, it seems to work quite well.
     
  10. Deleted User

    Deleted User

    Guest

    What is ScreenPosition? I can't find any reference to it in the docs.
     
  11. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Sorry, its a custom Rect specified at the top of our script, it's a bit messy, but it works.
     
  12. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Uhhh... yes they are.

    A unit is just a single piece of measurement that can be counted up.

    Macaronis are a unit of measurement if I measured my table by macaroni... it'd be stupid, but I COULD do it, in the real world.

    My computer screen has actual little pixels in it. I could count them up. I can measure by them. My monitor is native, physical, real world, 2560 LED tri-clusters that make a pixel, wide. And 1600 tall.


    As for OP.

    You say you want the resolution of the entire computer screen. Even if you're in a window. But you say that you're using 'Screen.currentResolution' for the window size?

    http://docs.unity3d.com/ScriptReference/Screen-currentResolution.html

    But that returns the resolution of the entire screen. Just tested... even in window mode it returns 2560x1600 for me... I resize the window, still the same output.
     
    SF_FrankvHoof likes this.
  13. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    @lordofduct this is what I want, I was trying to find the resolution of the screen, NOT the window, so the result would not change when the WINDOW was resized.
     
  14. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    So, you have the answer to your question?
     
  15. Deleted User

    Deleted User

    Guest

    I'm confused. :confused:
    Why are you using your custom "ScreenPosition" rect instead of Unity's Screen.currentResolution?
     
  16. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Because Screen.currentResolution returns the resolution of the Window, not the screen itself, it's just bad terminology on their part, so if the game is not in fullscreen, it will return the incorrect value.
     
    eneroth3 likes this.
  17. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    If it's doing that on your computer, that's odd.

    Cause I tested it on mine. And it returned the current resolution of the screen. I said this earlier.

    see:
    currentResolution.png
     
  18. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Odd, for me this is returning 1920x1080 and 3840x1080 when I have Surround turned on, the window is still 1920x1080 at the point though.
     
  19. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Change the window's size... and I bet the value will still remain the screen resolution.
     
  20. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Same result as last time, I am using something slightly different to you.
     
  21. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Stop using something slightly different. And use the Screen.currentResolution property.

    Of course if you use something different than that, it's going to be something different.

    Show me your code, what are you doing that's so different? We can't operate on these ambiguous statements you make.
     
    Bunny83 likes this.
  22. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    That's the most pedantic argument I've ever heard. And a completely pointless one, obviously technically you're correct but nobody uses pixels as a real world measurement so it's NOT a real world unit of measurement.
     
  23. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    They don't?

    You never heard "my tv is 1080p", or "4k tv's are coming out", or "my phone has a 10 mega-pixel camera".... not real world?
     
  24. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Not as a real world unit of measurement, no. e.g. You can't say, my living room is "4 1080p" TV's wide.
     
  25. ensiferum888

    ensiferum888

    Joined:
    May 11, 2013
    Posts:
    317
    It's a real world unit of measurement when we're talking about screen resolution though.
     
    Nad_B, ipodtouch0218, Tymac and 2 others like this.
  26. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Yeah, and I don't say my living room is 144 ounces wide either. Nor my monitor's resolution 100 degrees Celsius.

    Measurements have context.



    Also.. I'm not sure what "4 1080p" would even be. 1080p isn't a unit of measurement, it's a measurment, in pixels. That'd be like saying "4 10 acres" or "4 5 feet". Is that 4 measurements of 1080p in width? See... it's not the 1080p that doesn't make sense there, it's the application of it to width 4 times.
     
    Last edited: Apr 20, 2015
    ipodtouch0218, Tymac and rahulk1991 like this.
  27. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Thanks for helping me make my point! :D

    Cheers
     
  28. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    ...

    yeah, cheers mate

    o_O

    ...

    you do know that celsius and ounces are real world units of measurements right?
     
  29. Glordim

    Glordim

    Joined:
    Feb 8, 2015
    Posts:
    2
  30. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    FWIW - double check that outside the Editor. Screen stuff sometimes just straight up doesn't work.
     
  31. quizcanners

    quizcanners

    Joined:
    Feb 6, 2015
    Posts:
    109
    Found this while trying to solve my own problem: could be similar to this: https://forum.unity.com/threads/screen-width-is-wrong-in-editor-mode.94572/

    Screen.width and Screen.height do return Window Resolution when are called from inside the custom Inspector script. (my last post)

    _________________
    As for the discussion that plagued this topic: pixels are a Unit of measurement:
    Pixels, abbreviated as "px", are also a unit of measurement commonly used in graphic and web design, equivalent to roughly 1⁄96 inch (0.26 mm).

    The confusion arose because the Author said:
    "the physical size of the screen, in pixels"
    Makes sense to specify because we are working on a screen representation inside of a Game window, but the physical size of a screen is more commonly represented by inches/centimetres, while pixels can have different size.
    Probably should have formed it like:
    "The resolution of my physical screen, in pixels".
    Still, the first phrasing was only technically wrong, while conveying the point well enough. In contrast, the following comment was completely false. The author was probably thinking that pixels(in monitor/TV) have different size, so they can't be a unit of measurement, but we are not using those pixels to measure length or distance, but a resolution - which in turn represent how detailed of an image the screen can show. Many things are a unit of measurement:

    Bogosity: Lenat
    The unit of bogosity, i.e. how bogus a person, claim, or proceeding is, derived from the fictional field of Quantum Bogodynamics, is the Lenat. The Lenat is seldom used, as it is understood that it is too large for normal conversation. Its most common form is the microLenat.
     
    orionsyndrome likes this.
  32. placebo_yue

    placebo_yue

    Joined:
    Jan 4, 2017
    Posts:
    3
    Jesus christ what a load of BS on this thread. Stick to replying to the technical questions and stop discussing about measurements and linguistic S*** about what a pixel is.

    This line works if i start the game windowed, i use it on awake and it suddenly snaps my game to full screen native resolution.

    Screen.SetResolution(Screen.currentResolution.width, Screen.currentResolution.height, true);

    but it's possible that it doesn't work as intended on screens with different aspect ratios than mine and sadly i can't test that now
     
    Hades714, NIOS and brando_slc like this.
  33. GreenNinjaDa

    GreenNinjaDa

    Joined:
    Oct 8, 2020
    Posts:
    1
    Anyone looking for the answer to the original question of this thread, this will probably help you.
     
    IceCone_Topy and Good_Punk like this.
  34. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    @Glordim - thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you

    NOTE there is some weird behavior regarding mac retina ..

    if you LAUNCH WINDOWED, it will report the "half width" (say 1792)

    CHANGE the app to FULLSCREEN (ie, tap apple-F) and it will now report the correct figure (say 3584)

    HOWEVER

    if you RETURN to WINDOWED it will FROM NOW ON report the correct figure, ie 3584

    EXCEPTION

    If you LAUNCH IN fullscreen it will THEN only show the HALF value (eg 1792)

    So it's a total disaster

    This is absolutely a Unity bug.
     
    Last edited: Jan 7, 2021
    IceCone_Topy and Starburst999 like this.
  35. Tortuap

    Tortuap

    Joined:
    Dec 4, 2013
    Posts:
    137
    Stop being F***ing annoying with your post style ( font size, font bold, font color ).
    That's really really pissing me off.
     
    Leniaal likes this.
  36. brando_slc

    brando_slc

    Joined:
    Jan 4, 2017
    Posts:
    7
    Want a tissue?
     
    Hades714, NIOS and esco1979 like this.
  37. pfdaniel

    pfdaniel

    Joined:
    May 3, 2020
    Posts:
    14
    Did you ever find a solution to this retina issue? we're running into the same problem (2019 LTS)
     
  38. yuriythebest

    yuriythebest

    Joined:
    Nov 21, 2009
    Posts:
    1,125
    Hi Guys! Has this issue ever been fixed? I'm using Unity 2020.3.25f, and even though my resolution is 1920x1080, when I launch the game both Screen.currentResolution and Screen.Width+Screen.height return a smaller resolution.
    All I want to do is have the game start automatically at native - I have the "Default is Native Resolution" checkbox checked, not sure what more I can do...
     
  39. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    They have never fixed it or even addressed it and they likely never will.
     
  40. yuriythebest

    yuriythebest

    Joined:
    Nov 21, 2009
    Posts:
    1,125
    thanks! What I've ended up doing is initially forcing the resolution to 1920x1080, and then having an options screen where you can choose from various resolutions, instead of trying to have the game "figure out" what the resolution is.
     
  41. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,147
    Is Screen.currentResolution always returning the desktop resolution when windowed? If so just start in windowed mode, record the resolution, and then immediately call Screen.SetResolution().
     
    Last edited: Sep 30, 2022
  42. INeatFreak

    INeatFreak

    Joined:
    Sep 12, 2018
    Posts:
    46
    Here's how to access all the resolutions:

    // resolution in windowed mode
    Screen.width
    Screen.height

    // resolution set in the operating system
    Screen.currentResolution.width
    Screen.currentResolution.height

    // EDIT: The code below is actually false! It just returns same as Screen.width & height.
    // native resolution of the monitor
    Display.main.systemWidth
    Display.main.systemHeight


    Let me know if any of are incorrect, I've haven't got a chance to test all of them in build.
     
    Last edited: Jan 6, 2023
    kamicazer7 and assertor like this.
  43. Klausbdl

    Klausbdl

    Joined:
    Aug 12, 2013
    Posts:
    64
    I can vouch for that.

    What I did was get all resolutions and then use the last one as the "native" one for my calculations LOL
    Code (CSharp):
    1. resolutions = Screen.resolutions.ToList();
    2. resolutions = resolutions.GroupBy(resolution => new { resolution.width, resolution.height }).Select(group => group.First()).ToList();
    3. Debug.Log($"native resolution: {resolutions[^1].width} {resolutions[^1].height}");