Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Screen.resolutions returns duplicate entries on Unity 5.5.3

Discussion in 'Scripting' started by Michael-Ryan, Apr 25, 2017.

  1. Michael-Ryan

    Michael-Ryan

    Joined:
    Apr 10, 2009
    Posts:
    122
    I'm testing my project on Unity 5.5.3f1, and Screen.resolutions is returning some odd values.

    In Unity 5.4.2, Screen.resolutions returned just "640x480" when run in the editor. However, in Unity 5.5.3f1, it's returning what looks like all resolutions my display supports, but there are duplicate entries for most of them.

    In the attached image, you will notice that there are three entries for "720 x 480", "1152 x 864", "1280 x 720", and so on. Some entries, such as "800 x 600" are not duplicated.

    I suspected it had to do with the fact that my system has three displays, but the results were even worse on another workstation that has just two displays. On that system, there were a total of 208 resolutions in the array returned by Screen.resolutions, and many of them were duplicated 11-12 times.

    Can anyone confirm this issue on Unity 5.5.3f1? I couldn't find any mention of it on the forum or in release/patch notes.

    Thanks!
     

    Attached Files:

  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,356
    Confirmed that this is still an issue on 5.6.0f3. I did the following to build a unique, sorted list of available resolutions (note: for our purposes we don't use resolutions below 1024x768 so if you want them just omit the Where predicate on line 10
    Code (csharp):
    1.  
    2. static string[] resPresentation;
    3. static Resolution[] availResolutions;
    4. /// <summary>
    5. /// Caches a unique list resolutions available on the currently active display
    6. /// </summary>
    7. public static void StoreAvailableResolutions()
    8. {
    9.     // process only resolutions above 1024x768
    10.     var resList = UnityEngine.Screen.resolutions.Where(r => r.width > 1024 && r.height > 768);
    11.     // for some reason Unity contains duplicate entries for available resolutions so grab only unique ones
    12.     var unique = new Dictionary<string, Resolution>();
    13.     foreach (var res in resList)
    14.     {
    15.         unique[res.ToString()] = res;
    16.     }
    17.     // sort unique keys by comparing width and then height of their representative resolutions
    18.     var sortedKeys = unique.Keys.ToList();
    19.     sortedKeys.Sort((a, b) => {
    20.         int diff = unique[a].width.CompareTo(unique[b].width);
    21.         if (diff != 0) return diff;
    22.         return unique[a].height.CompareTo(unique[b].height);
    23.     });
    24.     availResolutions = new Resolution[sortedKeys.Count];
    25.     resPresentation = new string[sortedKeys.Count];
    26.     for (int i = 0; i < sortedKeys.Count; i++)
    27.     {
    28.         resPresentation[i] = sortedKeys[i];
    29.         availResolutions[i] = unique[sortedKeys[i]];
    30.     }
    31. }
    32.  
    I also filed a bug report to either clarify in the documentation if this is intentional and the unique entries have distinct meaning or to fix a bug in the API that returns duplicate values.

    Edit: bug report is here - https://fogbugz.unity3d.com/default.asp?907649_ejnidsk0jupgq960
     
    WalrusNine likes this.
  3. ShilohGames

    ShilohGames

    Joined:
    Mar 24, 2014
    Posts:
    1,705
    I can confirm this bug also exists in Unity 5.5.3p2. Screen.resolutions returns several duplicates of each available screen resolution.
     
  4. aaro4130

    aaro4130

    Joined:
    Jul 15, 2010
    Posts:
    366
    Confirmed on 5.6.2f1. Had a combo box going offscreen because of it. When I sorted out the duplicates there were MUCH less.
     
  5. SwiftWings42

    SwiftWings42

    Joined:
    Jan 25, 2014
    Posts:
    1
    The duplicate entries are different, though I agree that could be documented better. The duplicate entries have different framerates, which becomes apparent if you use .ToString() on the resolutions (and display them in a container wide enough to not cut off the @ whateverHz). For some reason they only show up when I build a standalone, in the editor it seems to limit itself to resolutions at 60Hz.
     
  6. aaro4130

    aaro4130

    Joined:
    Jul 15, 2010
    Posts:
    366
    @SwiftWings42 it looks like the issue is fixed. The issue was that you would get several repeats of the same thing.
    e.g
    1024x768 @60hz
    1024x768 @60hz
    1024x768 @60hz
    1024x768 @60hz
    640x480 @60hz
    640x480 @60hz

    and the list would go on.
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,356
    The bug I logged is still open but maybe it's fixed in 2017.1.

    And no - the refresh rates were not different. In fact, my solution relies on ToString() returning an identical string in order to build a set of unique values.
     
  8. x70x

    x70x

    Joined:
    Aug 20, 2011
    Posts:
    43
    I'm still experiencing this bug in 2017.1.
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    3,550
    The only thing I can note about that bug is that you can use a work-around of storing the "most recent" resolution string and if the next is the same, skip it.. until it's new.
    Just adding my 2cents as I browsed this thread to see what was the fuss ;)
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,356
    I literally posted a working solution, why further confuse the issue?
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    3,550
    Hey, that was unintentional..
    I did read the thread, including your answer, but then I spaced off when I was here and responded with what came to mind :) My bad.
     
  12. x70x

    x70x

    Joined:
    Aug 20, 2011
    Posts:
    43
    Unfortunately, the solution posted by KelsoMRK is not working for me either.
     
  13. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,356
    Don't rely on it working inside the Editor - it seems screen related things are generally screwy there. Do a build to confirm.

    Also - my solution was done with the intention of putting the results in a dropdown. If you're using it for something else then your mileage may vary.
     
  14. mkgame

    mkgame

    Joined:
    Feb 24, 2014
    Posts:
    388
    If you want to set the resolution in game settings, then just do the following:

    Code (CSharp):
    1. void Resolution(bool forward) {
    2.             Resolution old = Screen.resolutions[CurrentRS];
    3.             if (forward) {
    4.                 if (CurrentRS < Screen.resolutions.Length - 1) {
    5.                     CurrentRS++;
    6.                 } else {
    7.                     CurrentRS = 0;
    8.                 }
    9.             } else {
    10.                 if (CurrentRS > 0) {
    11.                     CurrentRS--;
    12.                 } else {
    13.                     CurrentRS = Screen.resolutions.Length - 1;
    14.                 }
    15.             }
    16.  
    17. // Recursively call the function itself until another resolution has been found.
    18.             if (Screen.resolutions[CurrentRS].width == old.width
    19.                 && Screen.resolutions[CurrentRS].height == old.height
    20.                 && Screen.resolutions[CurrentRS].refreshRate == old.refreshRate) {
    21.                 Resolution(forward);
    22.                 return;
    23.             }
    24.  
    25.             ResolutionText.text = Screen.resolutions[CurrentRS].width
    26.                 + " X " + Screen.resolutions[CurrentRS].height
    27.                 + " @ " + Screen.resolutions[CurrentRS].refreshRate;
    28.         }