Start Resolution Setting Script om Current Resolution?

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!

You can get the current resolution with Screen.currentResolution. You should be able to find the index of that in your array with resolutions.indexOf(Screen.currentResolution) assuming Resolution implements a reasonable Equals method (not guaranteed).

So how exactly would I implement the resolutions.indexOf(Screen.currentResolution); command? I tried putting this in the start function for trouble shooting…

print(resolutions.indexOf(Screen.currentResolution));

and it returned the following error messgae…

“Assets/ResolutionSetting.cs(52,27): error CS1061: Type UnityEngine.Resolution[ ]' does not contain a definition for indexOf’ and no extension method indexOf' of type UnityEngine.Resolution[ ]’ could be found. Are you missing an assembly reference?”

EDIT: Never mind! I figured out how to make the command work…

System.Array.IndexOf(resolutions, Screen.currentResolution);

Note to self: research before asking questions haha. Thank you for the help!

1 Like

Yep, sorry for the typo with IndexOf! Glad you got it working.

sorry to resurrect this old topic, but I’m using the same script as Treasureman, and used his new code but to no avail.

Here’s my modified start method:

    private void Start()
    {
        resolutions = Screen.resolutions;

        currentResolutionIndex = PlayerPrefs.GetInt(RESOLUTION_PREF_KEY, System.Array.IndexOf(resolutions, Screen.currentResolution));

        SetResolutionText(resolutions[currentResolutionIndex]);
    }

This works perfectly in the editor, it sets the index to the current screen resolution and sets the text correctly. However, when building the game, the text shows as blank until i hit the left or right buttons to cycle through the resolution options.

The only difference I can think of is the build shows all of the possible refresh rates, whereas the editor is just showing the current refresh rate.

The original script came from this video: