How to Change Screen Resolution in Unity Game

Changing the screen resolution within a Unity game after it has been built is a useful feature for enhancing user experience and compatibility across different devices. Unity provides methods to dynamically adjust screen resolution during runtime. In this tutorial, we'll guide you through the process of changing screen resolution in your Unity game and obtaining available screen resolutions.

Changing Screen Resolution during Runtime

Unity provides a straightforward API for changing screen resolution during gameplay. You can use the following code snippet to change the screen resolution:

using UnityEngine;

public class ResolutionManager : MonoBehaviour
{
    // Method to change screen resolution
    public void ChangeResolution(int width, int height, bool fullscreen)
    {
        Screen.SetResolution(width, height, fullscreen);
    }
}

To call this method and change the resolution, you can attach the 'ResolutionManager' script to a GameObject in your scene and invoke the 'ChangeResolution' method with the desired width, height, and fullscreen mode parameters.

Getting Available Screen Resolutions

Before allowing players to change the screen resolution, it's beneficial to provide them with a list of available resolutions. You can retrieve the available screen resolutions using the following code:

using UnityEngine;

public class ResolutionManager : MonoBehaviour
{
    // Method to get available screen resolutions
    public Resolution[] GetAvailableResolutions()
    {
        return Screen.resolutions;
    }
}

You can then use this method to populate a dropdown menu or list UI element in your game's settings menu, allowing players to choose their preferred resolution.

'ResolutionUI.cs'

using UnityEngine;
using UnityEngine.UI;

public class ResolutionUI : MonoBehaviour
{
    public Dropdown resolutionDropdown;
    public ResolutionManager resolutionManager;

    void Start()
    {
        // Populate dropdown with available resolutions
        Resolution[] resolutions = resolutionManager.GetAvailableResolutions();
        resolutionDropdown.ClearOptions();
        
        List<string> options = new List<string>();
        foreach (Resolution res in resolutions)
        {
            options.Add(res.width + "x" + res.height);
        }
        resolutionDropdown.AddOptions(options);
    }

    // Method to handle resolution change from UI
    public void OnResolutionChanged(int index)
    {
        Resolution[] resolutions = resolutionManager.GetAvailableResolutions();
        Resolution selectedResolution = resolutions[index];
        resolutionManager.ChangeResolution(selectedResolution.width, selectedResolution.height, true);
    }
}
  • Attach the 'ResolutionUI' script to a GameObject that contains a UI dropdown element ('Dropdown' component). Then, assign the dropdown and 'ResolutionManager' script references in the Unity Editor.

Conclusion

By following these steps and incorporating the provided code examples, you can seamlessly implement dynamic screen resolution changes in your Unity game, providing players with flexibility and enhancing their overall gaming experience.

Suggested Articles
How to Add Sniper Scope Effect in Unity
How to Pause Game in Unity
Creating a Puzzle Game in Unity
Creating a Pac-Man-Inspired Game in Unity
Creating a Traffic Simulator in Unity
Interacting with Objects in Unity Game
Creating a Bazooka in Unity