Unity-Technologies / DesktopSamples

This repository contains Unity samples for desktop platforms

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Off by one error

Sahasrara opened this issue · comments

    private void RefreshResolutions()
    {
        var resolutions = Screen.resolutions;

        // Filter out non-unique resolutions
        Array.Sort(resolutions, s_ResolutionComparer);

        int uniqueResolutionCount = 0;
        for (int i = 1; i < resolutions.Length; i++)
        {
            if (s_ResolutionComparer.Compare(resolutions[i], resolutions[uniqueResolutionCount]) != 0)
            {
                uniqueResolutionCount++;
                if (uniqueResolutionCount != i)
                    resolutions[uniqueResolutionCount] = resolutions[i];
            }
        }
       // --------- HERE!!!!!!
        if (uniqueResolutionCount == 0)
        {
            uniqueResolutionCount = 1;
            m_EmptyResolution[0] = Screen.currentResolution;
            resolutions = m_EmptyResolution;
        }

        var currentWidth = Screen.width;
        var currentHeight = Screen.height;
        int currentResolutionIndex = uniqueResolutionCount - 1;

        var resolutionDropdownOptions = m_ResolutionDropdown.options;
        MoveOptionsToCache(resolutionDropdownOptions);

        for (int i = 0; i < uniqueResolutionCount; i++)
        {
            var option = GetNewOption();
            var resolution = resolutions[i];
            option.text = GetCachedResolutionText(resolution.width, resolution.height);
            resolutionDropdownOptions.Add(option);

            if (resolution.width == currentWidth && resolution.height == currentHeight)
                currentResolutionIndex = i;
        }

        m_Resolutions = resolutions;
        m_ResolutionDropdown.SetValueWithoutNotify(currentResolutionIndex);
    }

I believe uniqueResolutionCount will always be 1 less than it should be because "i" starts a 1.

I don't think this is a bug, this is very intentional.

That for loop starts with an array sorted by resolution from highest to lowest. It then compares each one start with comparing index 1 to 0 and if there if it is not zero meaning it is not the same resolution, it will consider it found another unique resolution and increment that making uniqueResolutionCount 1 while i is still 1 so they will not be equal and it will not replace the uniqueResolutionCount index with index i value. This should continue to compare the next to the previous until it runs out of indexes in the array.

If uniqueResolutionCountis still zero after all that then it handles that situation, as in most situations it should have at least one.

So uniqueResolutionCountafter all is said and done may not always be 1 can be 0 or greater than 1 depending on the display and how many resolutions it supports.

I have stepped through this code and uniqueResolutionCount has never been 1. For my multi-monitor setup, each monitor is ending up with 25 unique resolutions.

Thanks for reporting this. I will take a look (I missed the initial report due to the holidays). I can't tell from the first glance if the code is incorrect.