using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; /// <summary> /// Provides access to changing the resolution of the game window /// </summary> public class ResolutionControl : MonoBehaviour { [SerializeField] private TMP_Dropdown resolutionDropdown; // Reference to the drop down setting window private Resolution[] resolutions; // Resolutions that were gathered from the user's system private List<Resolution> filteredResolutions; // Resolutions that are filtered base on the refresh rate private double currentRefreshRate; // The current refresh rate value private int currentResolutionIndex = 0; // The index in the filtered resolutions list that is active // Constant player prefs marker for the value within private const string Resolution = "Resolution"; private void Start() { // Reset options box and gather data resolutions = Screen.resolutions; filteredResolutions = new List<Resolution>(); resolutionDropdown.ClearOptions(); currentRefreshRate = Screen.currentResolution.refreshRateRatio.value; // If the value of the refresh rate of the system matches a gathered resolution's refresh rate, // it is added to the filtered list for (int i = 0; i < resolutions.Length; i++) { if (resolutions[i].refreshRateRatio.value == currentRefreshRate) filteredResolutions.Add(resolutions[i]); } // Lists all of the options for resolutions for the user's system List<string> options = new List<string>(); for (int i = 0; i < filteredResolutions.Count; i++) { string resolutionOption = $"{filteredResolutions[i].width}x{filteredResolutions[i].height} {filteredResolutions[i].refreshRateRatio.value}hz"; options.Add(resolutionOption); if (filteredResolutions[i].width == Screen.width && filteredResolutions[i].height == Screen.height) currentResolutionIndex = i; } // Finally gather the player prefs to set the current resolution index value, // otherwise a default resolution index is set while initializing the player Resolution try { currentResolutionIndex = int.Parse(PlayerPrefs.GetString(Resolution)); resolutionDropdown.AddOptions(options); resolutionDropdown.value = currentResolutionIndex; resolutionDropdown.RefreshShownValue(); } catch { PlayerPrefs.SetString(Resolution, currentResolutionIndex.ToString()); resolutionDropdown.AddOptions(options); resolutionDropdown.value = currentResolutionIndex; resolutionDropdown.RefreshShownValue(); } finally { PlayerPrefs.Save(); } } /// <summary> /// Sets the new resolution index value base on the field chosen in the drop down /// </summary> /// <param name="resolutionIndex">Dynamic Value: The value of the field in the drop down menu</param> public void SetResolution(int resolutionIndex) { // GUI function to indicate the resolution index that should be used now Resolution resolution = filteredResolutions[resolutionIndex]; Screen.SetResolution(resolution.width, resolution.height, true); currentResolutionIndex = resolutionIndex; PlayerPrefs.SetString(Resolution, currentResolutionIndex.ToString()); PlayerPrefs.Save(); } }