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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Multiple duplicates problem in resolutions

Discussion in 'Scripting' started by joxthebest314, May 22, 2020.

  1. joxthebest314

    joxthebest314

    Joined:
    Mar 31, 2020
    Posts:
    95
    I am trying to do a resolutions manager and everything works pretty well, except the fact that every resolutions appears multiple time in the dropdown...
    upload_2020-5-22_11-17-45.png

    I tried via code to delete the duplicates, but it doesn't work and I absolutely don't understand why.

    Code (CSharp):
    1. public class ResolutionsManager : MonoBehaviour
    2. {
    3.  
    4.     public Dropdown resolutionDropdown;
    5.  
    6.     public Resolution[] resolutions;
    7.  
    8.     public Resolution[] resolutionWithDuplicates;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         resolutionWithDuplicates = Screen.resolutions;
    14.  
    15.         List<Resolution> resolutionList = new List<Resolution>();
    16.         foreach(Resolution resolution in resolutionWithDuplicates)
    17.         {
    18.             if(!resolutionList.Contains(resolution))
    19.             {
    20.                 resolutionList.Add(resolution);
    21.             }
    22.         }
    23.  
    24.         resolutions = resolutionList.ToArray();
    25.  
    26.         resolutionDropdown.ClearOptions();
    27.  
    28.         List<string> options = new List<string>();
    29.  
    30.         int currentResoltionIndex = 0;
    31.  
    32.         for(int i = 0; i<resolutions.Length; i++)
    33.         {
    34.             string option = resolutions[i].width + "x" + resolutions[i].height;
    35.             options.Add(option);
    36.  
    37.             if(resolutions[i].width == Screen.width && resolutions[i].height == Screen.height)
    38.             {
    39.                 currentResoltionIndex = i;
    40.             }
    41.         }
    42.  
    43.         resolutionDropdown.AddOptions(options);
    44.         resolutionDropdown.value = currentResoltionIndex;
    45.         resolutionDropdown.RefreshShownValue();
    46.     }
    47.  
    48.  
    49.     public void SetResolution(int resolutionIndex)
    50.     {
    51.         Resolution resolution = resolutions[resolutionIndex];
    52.         Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen);
    53.        
    54.     }
    55. }
    Does anyone has an answer ?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    joxthebest314 likes this.
  3. joxthebest314

    joxthebest314

    Joined:
    Mar 31, 2020
    Posts:
    95
    Thank you so much, that was the issue.

    It's really optional, but is there a way to remove the "@" (not really important, but if you know).
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    build your own string using the three values, like you were doing before. Just include the refresh rate this time.
     
    joxthebest314 likes this.
  5. joxthebest314

    joxthebest314

    Joined:
    Mar 31, 2020
    Posts:
    95
    Thank you very much :)
     
  6. williamhiciano26

    williamhiciano26

    Joined:
    Feb 24, 2021
    Posts:
    1
    hey, i have the same issue just that i dont know where to put that line of code, (i have the exact same code as him)