Screen.resolutions returns duplicate entries on Unity 5.5.3

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!

4 Likes

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

static string[] resPresentation;
static Resolution[] availResolutions;
/// <summary>
/// Caches a unique list resolutions available on the currently active display
/// </summary>
public static void StoreAvailableResolutions()
{
    // process only resolutions above 1024x768
    var resList = UnityEngine.Screen.resolutions.Where(r => r.width > 1024 && r.height > 768);
    // for some reason Unity contains duplicate entries for available resolutions so grab only unique ones
    var unique = new Dictionary<string, Resolution>();
    foreach (var res in resList)
    {
        unique[res.ToString()] = res;
    }
    // sort unique keys by comparing width and then height of their representative resolutions
    var sortedKeys = unique.Keys.ToList();
    sortedKeys.Sort((a, b) => {
        int diff = unique[a].width.CompareTo(unique[b].width);
        if (diff != 0) return diff;
        return unique[a].height.CompareTo(unique[b].height);
    });
    availResolutions = new Resolution[sortedKeys.Count];
    resPresentation = new string[sortedKeys.Count];
    for (int i = 0; i < sortedKeys.Count; i++)
    {
        resPresentation[i] = sortedKeys[i];
        availResolutions[i] = unique[sortedKeys[i]];
    }
}

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

1 Like

I can confirm this bug also exists in Unity 5.5.3p2. Screen.resolutions returns several duplicates of each available screen resolution.

Confirmed on 5.6.2f1. Had a combo box going offscreen because of it. When I sorted out the duplicates there were MUCH less.

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.

1 Like

@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.

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.

I’m still experiencing this bug in 2017.1.

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 :wink:

I literally posted a working solution, why further confuse the issue?

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 :slight_smile: My bad.

Unfortunately, the solution posted by KelsoMRK is not working for me either.

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.

If you want to set the resolution in game settings, then just do the following:

void Resolution(bool forward) {
            Resolution old = Screen.resolutions[CurrentRS];
            if (forward) {
                if (CurrentRS < Screen.resolutions.Length - 1) {
                    CurrentRS++;
                } else {
                    CurrentRS = 0;
                }
            } else {
                if (CurrentRS > 0) {
                    CurrentRS--;
                } else {
                    CurrentRS = Screen.resolutions.Length - 1;
                }
            }

// Recursively call the function itself until another resolution has been found.
            if (Screen.resolutions[CurrentRS].width == old.width
                && Screen.resolutions[CurrentRS].height == old.height
                && Screen.resolutions[CurrentRS].refreshRate == old.refreshRate) {
                Resolution(forward);
                return;
            }

            ResolutionText.text = Screen.resolutions[CurrentRS].width
                + " X " + Screen.resolutions[CurrentRS].height
                + " @ " + Screen.resolutions[CurrentRS].refreshRate;
        }

I just noticed this issue too. You can also use Linq to filter for uniqueness.

using System.Linq;


var availableResolutions = Screen.resolutions.Distinct().ToArray();

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.

using System.Linq;

var resolutions = Screen.resolutions.Distinct().OrderBy(x => x.width)
            .ThenBy(x => x.height).ThenBy(x => x.refreshRate).ToArray();
1 Like

HahahaaaHaaa… hahahaaaaHaaa~…
HAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, UNITY 2022.2.0a17, BABYYYYYYYY–
8226303--1074585--upload_2022-6-23_2-13-57.png

Please don’t necro posts, especially in such a silly way.

2 Likes