- Home /
[Solved] Screen.resolutions returning duplicates
In the editor, Screen.resolutions functions as expected. The problem is on standalone where it appears to duplicate each resolution with refresh rates of 60Hz and 48Hz. Is there any way to only use 60Hz refresh rates or completely omit refresh rates when getting Screen.resolutions?
This is the Screen.resolutions log when cycling through resolutions: 1920 x 1080 @ 60Hz 1920 x 1080 @ 48Hz 1680 x 1050 @ 60Hz 1680 x 1050 @ 48Hz 1600 x 900 @ 60Hz 1600 x 900 @ 48Hz And so on...
Answer by FlaSh-G · Feb 03, 2018 at 05:52 PM
You can filter out all non-60hz results after calling the function.
A Linq statement like this would work:
var resolutions = Screen.resolutions.Where(resolution => resolution.refreshRate == 60);
Either way - remember that the hz number is not unimportant. Especially for people using monitors that go above 60hz.
Also: You might have different results on different computers. My test just gave me a lot of resolutions, but none of them were 60hz. So if you want to just have unique height and width results, filter for that.
var resolutions = Screen.resolutions.Select(resolution => new Resolution { width = resolution.width, height = resolution.height }).Distinct();
Be aware that this statement will delete the hz info completely.
Remember to use
using System.Linq;
When I try to use that function with an array I get this error: error CS0266: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<UnityEngine.Resolution>' to 'UnityEngine.Resolution[]'. An explicit conversion exists (are you missing a cast?)
$$anonymous$$y resolution system relies on the array of resolutions that Screen.resolutions puts out. Any idea how I could incorporate said Linq function into that system?
Put this
.ToArray()
afterwards to make an array out of it.
Well that solution was simpler than what I was expecting. Anyway, everything is working as intended now, thank you for the help!
Using this in 2020 this is a bad idea. Screens with refresh rates over 60Hz are beco$$anonymous$$g mainstream.
$$anonymous$$aybe it may not be a bad idea as SetResolution will use the highest refresh rate supported by the monitor by default but yes the user loses the option to choose a specific refresh rate. The best option would be to have a drop-down showing the resolution without the refresh rate and another drop-down to choose the refresh rate.
Answer by FZ_Applications · Mar 30, 2020 at 04:27 PM
More and more high refresh rate monitors are being used. Keeping the player stuck at 60Hz is not good. This method filters out low refresh rates.
public static List<Resolution> GetResolutions() {
//Filters out all resolutions with low refresh rate:
Resolution[] resolutions = Screen.resolutions;
HashSet<Tuple<int, int>> uniqResolutions = new HashSet<Tuple<int, int>>();
Dictionary<Tuple<int, int>, int> maxRefreshRates = new Dictionary<Tuple<int, int>, int>();
for (int i = 0; i < resolutions.GetLength(0); i++) {
//Add resolutions (if they are not already contained)
Tuple<int, int> resolution = new Tuple<int, int>(resolutions[i].width, resolutions[i].height);
uniqResolutions.Add(resolution);
//Get highest framerate:
if (!maxRefreshRates.ContainsKey(resolution)) {
maxRefreshRates.Add(resolution, resolutions[i].refreshRate);
} else {
maxRefreshRates[resolution] = resolutions[i].refreshRate;
}
}
//Build resolution list:
List<Resolution> uniqResolutionsList = new List<Resolution>(uniqResolutions.Count);
foreach (Tuple<int, int> resolution in uniqResolutions) {
Resolution newResolution = new Resolution();
newResolution.width = resolution.Item1;
newResolution.height = resolution.Item2;
if(maxRefreshRates.TryGetValue(resolution, out int refreshRate)) {
newResolution.refreshRate = refreshRate;
}
uniqResolutionsList.Add(newResolution);
}
return uniqResolutionsList;
}
Your answer
Follow this Question
Related Questions
Remove 800x500 From Build Configuration 0 Answers
Ideal resolutions for 2D platformer Art Assets (to make multi-res support easier)? 2 Answers
Is there any way to change Preview resolution by code? 2 Answers
After upgrading to 2018.2.6f1 full screen resolution is wrong 1 Answer
UGUI Aspect Ratios and Resolutions 1 Answer