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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Duplicate resolution problem and Add a call function "Set default" on a settings scripts

Discussion in 'Scripting' started by Shyno19, Dec 6, 2020.

  1. Shyno19

    Shyno19

    Joined:
    Aug 29, 2020
    Posts:
    3
    Hi
    I find a script on net for adding a settings résolution.
    But the problem is :
    -When i build for test, the resolution are duplicate by 3

    -I want to add a call function like : public void SetDefault, so I could call it by a button and set the resolution that i would put in this function.

    There is the script :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEditor;
    6.  
    7. public class ResolutionMenu : MonoBehaviour
    8. {
    9.     private const string resolutionWidthPlayerPrefKey = "ResolutionWidth";
    10.     private const string resolutionHeightPlayerPrefKey = "ResolutionHeight";
    11.     private const string resolutionRefreshRatePlayerPrefKey = "RefreshRate";
    12.     private const string fullScreenPlayerPrefKey = "FullScreen";
    13.     public Toggle fullScreenToggle;
    14.     public Dropdown resolutionDropdown;
    15.     Resolution[] resolutions;
    16.     Resolution selectedResolution;
    17.     void Start()
    18.     {
    19.         resolutions = Screen.resolutions;
    20.         LoadSettings();
    21.         CreateResolutionDropdown();
    22.  
    23.         fullScreenToggle.onValueChanged.AddListener(SetFullscreen);
    24.         resolutionDropdown.onValueChanged.AddListener(SetResolution);
    25.     }
    26.  
    27.     private void LoadSettings()
    28.     {
    29.         selectedResolution = new Resolution();
    30.         selectedResolution.width = PlayerPrefs.GetInt(resolutionWidthPlayerPrefKey, Screen.currentResolution.width);
    31.         selectedResolution.height = PlayerPrefs.GetInt(resolutionHeightPlayerPrefKey, Screen.currentResolution.height);
    32.         selectedResolution.refreshRate = PlayerPrefs.GetInt(resolutionRefreshRatePlayerPrefKey, Screen.currentResolution.refreshRate);
    33.  
    34.         fullScreenToggle.isOn = PlayerPrefs.GetInt(fullScreenPlayerPrefKey, Screen.fullScreen ? 1 : 0) > 0;
    35.  
    36.         Screen.SetResolution(
    37.             selectedResolution.width,
    38.             selectedResolution.height,
    39.             fullScreenToggle.isOn
    40.         );
    41.     }
    42.  
    43.     private void CreateResolutionDropdown()
    44.     {
    45.         resolutionDropdown.ClearOptions();
    46.         List<string> options = new List<string>();
    47.         int currentResolutionIndex = 0;
    48.         for (int i = 0; i < resolutions.Length; i++)
    49.         {
    50.             string option = resolutions[i].width + " x " + resolutions[i].height;
    51.             options.Add(option);
    52.             if (Mathf.Approximately(resolutions[i].width, selectedResolution.width) && Mathf.Approximately(resolutions[i].height, selectedResolution.height))
    53.             {
    54.                 currentResolutionIndex = i;
    55.             }
    56.         }
    57.         resolutionDropdown.AddOptions(options);
    58.         resolutionDropdown.value = currentResolutionIndex;
    59.         resolutionDropdown.RefreshShownValue();
    60.     }
    61.  
    62.     public void SetFullscreen(bool isFullscreen)
    63.     {
    64.         Screen.fullScreen = isFullscreen;
    65.         PlayerPrefs.SetInt(fullScreenPlayerPrefKey, isFullscreen ? 1 : 0);
    66.     }
    67.     public void SetResolution(int resolutionIndex)
    68.     {
    69.         selectedResolution = resolutions[resolutionIndex];
    70.         Screen.SetResolution(selectedResolution.width, selectedResolution.height, Screen.fullScreen);
    71.         PlayerPrefs.SetInt(resolutionWidthPlayerPrefKey, selectedResolution.width);
    72.         PlayerPrefs.SetInt(resolutionHeightPlayerPrefKey, selectedResolution.height);
    73.         PlayerPrefs.SetInt(resolutionRefreshRatePlayerPrefKey, selectedResolution.refreshRate);
    74.     }
    75. }
    76.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    There are multiple resolution options with the same resolution because the Resolution struct also includes a refresh rate. For example you might have 3 resolution options like this:
    Code (CSharp):
    1. 1920x1080@60hz
    2. 1920x1080@59.94hz
    3. 1920x1080@144hz
     
  3. Shyno19

    Shyno19

    Joined:
    Aug 29, 2020
    Posts:
    3
    I suspected it, how i can fix it ?