Bug with screen.resolutions that is working only on the editor :/

Hi guys, i have a problem with Unity 2017.1 :confused: as you can see on the screen my simple testy code with
screen.resolutions isnt working :confused:

Look in editor and Build

(Editor work properly and Build print the same resolution ~3times)

 Rect winRect;

    void OnGUI()
    {
        winRect = GUILayout.Window(0, winRect, OnWindow, "Resolutions");
    }

    void OnWindow(int ID)
    {
        GUI.DragWindow();
        GUILayout.BeginVertical();
        foreach (Resolution R in Screen.resolutions)
        {
            GUILayout.Label("Res: " + R.width + "x" + R.height, GUILayout.MinWidth(200));
        }
        GUILayout.EndVertical();
    }



3170772–241555–ScreenResolutionBug.cs (607 Bytes)

This is a known bug: Screen.resolutions returns duplicate entries on Unity 5.5.3 - Unity Engine - Unity Discussions

Uh i’m not the only one ^^ :slight_smile: Do you know how to fix it? :confused:

[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete(“Property GetResolution has been deprecated. Use resolutions instead (UnityUpgradable) → resolutions”, true)]

I think this bug appear when unity was switching method to get screen resolutions :confused:

I literally posted a code solution in that thread :slight_smile:

Uh so it doesnt work for me :confused:

Its still doing the same on the editor and the build :confused:

Here is the code:

 void Start()
    {
        StoreAvailableResolutions();
        for (int i = 0; i < availResolutions.Length; i++)
        {
            ScreenResolution.options.Add(new Dropdown.OptionData() { text = availResolutions[i].width + "x" + availResolutions[i].height });
        }
    }

    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;
        // 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]];
        }
    }

Here is screens of what it does :confused: i read the code and it should work :confused:


Are you sure those aren’t actually the same dimensions with different refresh rates?

Nope :confused: already checked :frowning:

Uh okay its what you said but my text setup wasnt showing me the @ 60Hz… :frowning: sorry :confused: But do you know how to only print resolutions without their 60Hz, 70Hz i really want a list with every resolutions and no duplicates :confused: because i use
Screen.SetResolution(800,600,fullscreen,HERTZ) :confused:

Please :frowning:

3171008--241565--rthrt.PNG

Don’t use ToString() when creating the dictionary entries. Just combine width and height like you do in the dropdown

Thanks :wink: it work :smile: I love you :sweat_smile::sweat_smile::):smile::smile::smile:

static string[] ressolutions;
    public static void StoreAvailableResolutions()
    {
        var resList = UnityEngine.Screen.resolutions;
        var unique = new Dictionary<string, Resolution>();
        foreach (var res in resList)
        {
            unique[res.width+"x"+res.height] = res;
        }
        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);
        });
        ressolutions = new string[sortedKeys.Count];
        for (int i = 0; i < sortedKeys.Count; i++)
        {
            ressolutions[i] = sortedKeys[i];
        }
    }

1 Like