Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolution Drop down Menu, Duplicates because of refresh rate

Discussion in 'Scripting' started by khold93, Sep 22, 2020.

  1. khold93

    khold93

    Joined:
    Feb 11, 2020
    Posts:
    17
    Hello,

    I created a menu so players can change resolution.
    In the editor it's fine and only shows options of 165 hz refresh rate, but when I build the game I get a full list as you can see on the image that includes every refresh rate option.
    What is the best way around this?
    I want to have a list with only resolutions using the current refresh rate the user has. Just like it does in the editor.

     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
  3. khold93

    khold93

    Joined:
    Feb 11, 2020
    Posts:
    17
    How do I know what refresh rate I want? I don't know what sort of refresh rates people have. I don't mind 75, 90, 120 etc. I just want one option and thats the refresh rate they use in general.
    Do I have to go through them all and remove the lowest onces?
    Just seem like a thing that should be easier to get around.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Honestly, that sounds like a good argument not to filter out certain refresh rates to me. Why not give the user all the options?

    A few possibilities I can think of:
    1. Look at the current refresh rate from Screen.currentResolution: https://docs.unity3d.com/ScriptReference/Screen-currentResolution.html. Filter out any that aren't the same as that.
    2. Look at all of the resolutions from Screen.resolutions, pick the highest refresh rate, and only show resolutions with that highest rate.
    3. Maybe do a hybrid approach. Pick 1. or 2., but give the user a checkbox like "show all refresh rates", which will update the list to include all of them.
     
  5. khold93

    khold93

    Joined:
    Feb 11, 2020
    Posts:
    17
    Sounds good, I will try that way next time I'm working on the project :)
    thanks for good input!
    Just throwing it out there - The reason I don't want to show all is because people might have a list like mine, where its +140 different choices. It looks kinda bad :)
     
  6. souhilringo

    souhilringo

    Joined:
    Jul 11, 2020
    Posts:
    1
    how to filter the resolutions that contain the refrrshrate 45hz ?
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
  8. Mikhailzrick

    Mikhailzrick

    Joined:
    May 23, 2020
    Posts:
    4
    I'm running into the exact same problem. Did you figure out a solution that worked for you? I've tried different things to try and remove any refresh rates that don't match the current refresh rate but can't seem to get it to work. So I start working on other stuff and every time I come back to this problem I can't seem to make any progress.
     
  9. khold93

    khold93

    Joined:
    Feb 11, 2020
    Posts:
    17
    Hey, It's a old project I havne't touched in a while but this is what I found.
    The code ain't pretty, but it did the job. Hope it helps
    Lists:
    Code (CSharp):
    1.     List<Resolution> resolutions = new List<Resolution>();
    1. Add all resolutions in a list (distinct)

    Code (CSharp):
    1.     void AddResolutions()
    2.     {
    3.         if (resolutions == null || resolutions.Count == 0)
    4.         {
    5.             resolutions.AddRange(Screen.resolutions.ToList().Distinct());
    6.         }
    7.  
    8.         resolutions = resolutions.OrderByDescending(i => i.width).ToList();
    9.     }
    2. Fix Resolution dropdown menu

    Code (CSharp):
    1.     void FixResolutionDropdown()
    2.     {
    3.         List<string> options = new List<string>();
    4.         string option;
    5.         int currentResolutionIndex = 0;
    6.  
    7.         int refreshRate = refreshRates[refreshRateDropdown.value];
    8.         List<Resolution> resolutionsOptions = new List<Resolution>();
    9.         resolutionsOptions.AddRange(resolutions.Where(resolution => resolution.refreshRate == refreshRate).ToList());
    10.  
    11.         int i = 0;
    12.         foreach (Resolution resolution in resolutionsOptions)
    13.         {
    14.             option = resolution.width + " x " + resolution.height;// + ", " + resolution.refreshRate + " hz";
    15.             options.Add(option);
    16.             if (resolutionsOptions[i].width == Screen.width && resolutionsOptions[i].height == Screen.height)
    17.                 currentResolutionIndex = i;
    18.             i++;
    19.         }
    20.  
    21.         resolutionDropdown.ClearOptions();
    22.         resolutionDropdown.AddOptions(options);
    23.         resolutionDropdown.value = currentResolutionIndex;
    24.         resolutionDropdown.RefreshShownValue();
    25.     }
     
  10. lyorehd

    lyorehd

    Joined:
    Jan 24, 2021
    Posts:
    1
    Tried your scipt but it somehow didnt find refreshRates. Sry im new

    Can someone tell me what i made wrong pls

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using TMPro;
    using System.Linq;

    public class Options : MonoBehaviour
    {
    public TMP_Dropdown resolutionDropdown;

    public TMP_Dropdown refreshRateDropdown;

    List<Resolution> resolutions = new List<Resolution>();

    void AddResolutions()
    {
    if (resolutions == null || resolutions.Count == 0)
    {
    resolutions.AddRange(Screen.resolutions.ToList().Distinct());
    }

    resolutions = resolutions.OrderByDescending(i => i.width).ToList();
    }

    void FixResolutionDropdown()
    {
    List<string> options = new List<string>();
    string option;
    int currentResolutionIndex = 0;

    int refreshRate = refreshRates[refreshRateDropdown.value];
    List<Resolution> resolutionsOptions = new List<Resolution>();
    resolutionsOptions.AddRange(resolutions.Where(resolution => resolution.refreshRate == refreshRate).ToList());

    int i = 0;
    foreach (Resolution resolution in resolutionsOptions)
    {
    option = resolution.width + " x " + resolution.height;// + ", " + resolution.refreshRate + " hz";
    options.Add(option);
    if (resolutionsOptions.width == Screen.width && resolutionsOptions.height == Screen.height)
    currentResolutionIndex = i;
    i++;
    }

    resolutionDropdown.ClearOptions();
    resolutionDropdown.AddOptions(options);
    resolutionDropdown.value = currentResolutionIndex;
    resolutionDropdown.RefreshShownValue();
    }
     
  11. ilbailba

    ilbailba

    Joined:
    Nov 20, 2020
    Posts:
    1
    Hello there! I saw this here while I was working on this issue recently. khold93, could you please share with me the code you created to start this discussion?

    You wrote a really nice code and it would be great if I could reach that code right now.

    I would be very happy if you share the code with me. good day!

    (this =
    )
    -----------

     

    Attached Files: