Search Unity

Screen.resolutions returns duplicate entries on Unity 5.5.3

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

Thread Status:
Not open for further replies.
  1. Michael-Ryan

    Michael-Ryan

    Joined:
    Apr 10, 2009
    Posts:
    184
    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,539
    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:
    3,023
    I can confirm this bug also exists in Unity 5.5.3p2. Screen.resolutions returns several duplicates of each available screen resolution.
     
  4. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    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.
     
    WiseOldHooter likes this.
  6. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    @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,539
    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:
    49
    I'm still experiencing this bug in 2017.1.
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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,539
    I literally posted a working solution, why further confuse the issue?
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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:
    49
    Unfortunately, the solution posted by KelsoMRK is not working for me either.
     
  13. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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:
    592
    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.         }
     
  15. mpeddicord

    mpeddicord

    Joined:
    Mar 17, 2013
    Posts:
    13
    I just noticed this issue too. You can also use Linq to filter for uniqueness.


    Code (CSharp):
    1. using System.Linq;
    2.  
    3.  
    4. var availableResolutions = Screen.resolutions.Distinct().ToArray();
     
  16. Michael-Ryan

    Michael-Ryan

    Joined:
    Apr 10, 2009
    Posts:
    184
    I also used Linq to filter the results. I reordered them, however, because the original array began with my native resolution and then jumped to 640 x 480 before increasing again.

    Code (CSharp):
    1. using System.Linq;
    2.  
    3. var resolutions = Screen.resolutions.Distinct().OrderBy(x => x.width)
    4.             .ThenBy(x => x.height).ThenBy(x => x.refreshRate).ToArray();
     
    ZO5KmUG6R likes this.
  17. RetroeyDoofery

    RetroeyDoofery

    Joined:
    Mar 5, 2020
    Posts:
    1
    HahahaaaHaaa... hahahaaaaHaaa~....
    HAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, UNITY 2022.2.0a17, BABYYYYYYYY--
    upload_2022-6-23_2-13-57.png
     
  18. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Please don't necro posts, especially in such a silly way.
     
    RetroeyDoofery and Bunny83 like this.
Thread Status:
Not open for further replies.