Hello!
Recently, I’ve been working on implementing a runtime resolution setting to my game. The script I’m currently using works, but there are a few issues. The script will start on the lowest possible resolution (index 0) no matter what the player’s current resolution is. I’d like if the script could start it’s index on the current resolution, but I’m not sure how I’d get this. Could I somehow find which value of my index hold my current resolution so that I could make a variable holding that value? Here’s the code…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ResolutionSetting : MonoBehaviour {
public GameObject windowed;
private bool fullScreen;
#region Player Pref Key Constant
private const string RESOLUTION_PREF_KEY = "resolution";
#endregion
#region Resolution
[SerializeField]
private Text resolutionText;
private Resolution[] resolutions;
private int currentResolutionIndex;
#endregion
public bool gamepad;
public Button[] uiButtons;
public bool gamepadFalseLast;
public GameObject leftArrowHover;
public GameObject rightArrowHover;
public bool stickDownLast;
void Start()
{
//resolutionText.text = Screen.currentResolution.width + "x" + Screen.currentResolution.height + " @ " + Screen.currentResolution.refreshRate + "hz";
/*while(resolutionText.text != (Screen.currentResolution.width + "x" + Screen.currentResolution.height + " @ " + Screen.currentResolution.refreshRate + "hz"))
{
SetPreviousResolution();
}*/
resolutions = Screen.resolutions;
//currentResolutionIndex = PlayerPrefs.GetInt(RESOLUTION_PREF_KEY, 0);
SetResolutionText(resolutions[currentResolutionIndex]);
fullScreen = Screen.fullScreen;
}
#region Resolution Cycling
private void SetResolutionText(Resolution resolution)
{
resolutionText.text = resolution.width + "x" + resolution.height + " @ " + resolution.refreshRate + "hz";
//
}
public void SetNextResolution()
{
currentResolutionIndex = GetNextWrappedIndex(resolutions, currentResolutionIndex);
SetResolutionText(resolutions[currentResolutionIndex]);
}
public void SetPreviousResolution()
{
currentResolutionIndex = GetPreviousWrappedIndex(resolutions, currentResolutionIndex);
SetResolutionText(resolutions[currentResolutionIndex]);
}
#endregion
#region Apply Resolution
void Update()
{
if (windowed.activeSelf == true)
{
fullScreen = false;
}
if (windowed.activeSelf == false)
{
fullScreen = true;
}
if (Input.anyKey || Input.GetAxis("Mouse X Mouse") != 0 || Input.GetAxis("Mouse Y Mouse") != 0)
{
gamepad = false;
}
if (Input.GetAxis("Horizontal Arrow") != 0 || Input.GetAxis("Vertical Arrow") != 0 || Input.GetAxis("Horizontal Joystick") != 0 || Input.GetAxis("Vertical Joystick") != 0 || Input.GetButton("A") || Input.GetButton("B") || Input.GetButton("Start"))
{
gamepad = true;
}
if (gamepad == true)
{
gamepadFalseLast = false;
foreach (Button buttons in uiButtons)
{
buttons.enabled = false;
}
if (Input.GetAxisRaw("Horizontal Arrow") == 1 || Input.GetAxis("Horizontal Joystick") > 0)
{
if (stickDownLast == false)
{
if (leftArrowHover.activeSelf || rightArrowHover.activeSelf)
{
SetNextResolution();
stickDownLast = true;
}
}
}
if (Input.GetAxisRaw("Horizontal Arrow") == -1 || Input.GetAxis("Horizontal Joystick") < 0)
{
if (stickDownLast == false)
{
if (leftArrowHover.activeSelf || rightArrowHover.activeSelf)
{
SetPreviousResolution();
stickDownLast = true;
}
}
}
if (Input.GetAxisRaw("Horizontal Arrow") == 0 && Input.GetAxis("Horizontal Joystick") == 0)
{
stickDownLast = false;
}
}
if (gamepad == false)
{
foreach (Button buttons in uiButtons)
{
buttons.enabled = true;
}
if (gamepadFalseLast == false)
{
leftArrowHover.SetActive(false);
rightArrowHover.SetActive(false);
}
gamepadFalseLast = true;
}
}
private void SetAndApplyResolution(int newResolutionIndex)
{
currentResolutionIndex = newResolutionIndex;
ApplyCurrentResolution();
}
private void ApplyCurrentResolution()
{
ApplyResolution(resolutions[currentResolutionIndex]);
}
private void ApplyResolution(Resolution resolution)
{
SetResolutionText(resolution);
Screen.SetResolution(resolution.width, resolution.height, fullScreen);
//PlayerPrefs.SetInt(RESOLUTION_PREF_KEY, currentResolutionIndex);
}
#endregion
#region Misc Helpers
#region Index Wrap Helpers
private int GetNextWrappedIndex<T>(IList<T> collection, int currentIndex)
{
if (collection.Count < 1) return 0;
return (currentIndex + 1) % collection.Count;
}
private int GetPreviousWrappedIndex<T>(IList<T> collection, int currentIndex)
{
if (collection.Count < 1) return 0;
if ((currentIndex - 1) < 0) return collection.Count - 1;
return (currentIndex - 1) % collection.Count;
}
#endregion
#endregion
public void ApplyChange()
{
SetAndApplyResolution(currentResolutionIndex);
}
}
I got this primarily from a YouTube tutorial, and I’m really not sure why he set it up this way. Thank you to anybody who can help!