- Home /
How to make a refresh rate dropdown menu?
I made a resolution dropdown menu and I noticed that the resolution options would duplicate. I did some research and found out that that was because of different refresh rates on the monitor. I want to know if there is any way to display the refresh rate in the resolution dropdown so that the options don't look like duplicates. Or make a separate dropdown for refresh rates.
Here is my code for the resolution:
//.............Setting up Resolutions
resolutions = Screen.resolutions;
resolutionDropdown.ClearOptions();
List<string> options = new List<string>();
int currentResolutionIndex = 0;
for (int i = 0; i < resolutions.Length; i++)
{
string option = resolutions[i].width + "x" + resolutions[i].height;
options.Add(option);
if (resolutions[i].width == Screen.currentResolution.width &&
resolutions[i].height == Screen.currentResolution.height)
{
currentResolutionIndex = i;
}
}
resolutionDropdown.AddOptions(options);
resolutionDropdown.value = currentResolutionIndex;
resolutionDropdown.RefreshShownValue();
//.............End of Setting up Resolutions
Answer by Hellium · Sep 17, 2019 at 08:34 PM
I see several solutions (none of them have been tested)
You can check if the option is not already in the list:
for (int i = 0; i < resolutions.Length; i++)
{
string option = resolutions[i].width + "x" + resolutions[i].height;
if(options.Contains(option))
continue;
options.Add(option);
if (resolutions[i].width == Screen.currentResolution.width &&
resolutions[i].height == Screen.currentResolution.height)
{
currentResolutionIndex = i;
}
}
You can use a Hashset
HashSet<string> options = new HashSet<string>();
int currentResolutionIndex = 0;
for (int i = 0; i < resolutions.Length; i++)
{
string option = resolutions[i].width + "x" + resolutions[i].height;
options.Add(option);
if (resolutions[i].width == Screen.currentResolution.width &&
resolutions[i].height == Screen.currentResolution.height)
{
currentResolutionIndex = i;
}
}
resolutionDropdown.AddOptions( new List<string>(options));
resolutionDropdown.value = currentResolutionIndex;
resolutionDropdown.RefreshShownValue();
Use System.Linq
and a custom comparer to filter the resolutions when retrieving them
// Top of your script
using System.Linq;
public class ResolutionComparer : IEqualityComparer<Resolution>
{
public bool Equals( Resolution x, Resolution y )
{
return x.width.Equals( y.width ) && x.height.Equals( y.height );
}
public int GetHashCode( Resolution obj )
{
return obj.GetHashCode();
}
}
public class YOUR_CLASS_NAME : MonoBehaviour
{
// in your function
ResolutionComparer resolutionComparer = new ResolutionComparer();
resolutions = Screen.resolutions.Distinct( resolutionComparer ).ToArray();
resolutionDropdown.ClearOptions();
List<string> options = new List<string>();
int currentResolutionIndex = 0;
for ( int i = 0 ; i < resolutions.Length ; i++ )
{
string option = resolutions[i].width + "x" + resolutions[i].height;
options.Add( option );
if ( resolutionComparer.Equals(resolutions[i], Screen.currentResolution))
{
currentResolutionIndex = i;
}
}
resolutionDropdown.AddOptions( options );
resolutionDropdown.value = currentResolutionIndex;
resolutionDropdown.RefreshShownValue();
}
Thank you for replying. I used system.Linq and I think I filtered out the refresh rate information. Here is the line of code I used: resolutions = Screen.resolutions.Select(resolution => new Resolution { width = resolution.width, height = resolution.height }).Distinct().ToArray();. Will filtering out refresh rate information negatively effect different monitors?
What do you mean by filtering out negatively
?
Like if the game can only play at 60hz and someone else has a 144hz monitor. Will filtering out the refresh rate from the resolution dropdown effect a player's refresh rate or limit it?
Thank you!! I was running into the same problem and using the examples Hellium and ImmanuelUchennabest provided solved it. If you aren't using exclusive fullscreen(which I'm not) you can't change the refresh rate from the current desktop refresh rate. So I was ending up with unusable duplicate resolution values in my dropdown. This solved it. Thanks again! :)
Your answer
Follow this Question
Related Questions
I built a test and the resolution dropdown is not working, why? 0 Answers
how to change resolution in dropdown? 1 Answer
How to convert a list of Resolution to String 2 Answers
Multiple Cars not working 1 Answer
Remove specific screen resolutions 0 Answers