How To Change Screen Size at Runtime – Unity

This article shows you how to change the screen size at run time via script, while avoiding the unexpected pitfalls that can occur, as well as other topics, such as:

  • How to toggle full screen and windowed modes
  • How to limit the frame rate
  • How to toggle vsync on and off
  • How to set the resolution

Full Screen

We’re going to look at 3 methods for full screen toggling. I use method 3 in Puzzledorf but it’s worth looking at each one.

Method 1

There are several ways of going about this. Method 1 is a simple toggle on / off.

private void Update()
{
    if (Input.GetKeyDown(KeyCode.L))
    {            
        Screen.fullScreen = !Screen.fullScreen;
    }
}

Test that out in a script. Screen.fullscreen simply switches between Full Screen and Windowed. I think the Full Screen is actually Full Screen Windowed rather than Exclusive Full Screen. It’s simple, it works, but it doesn’t give you control over the resolution.

Method2

This method gives you finer control choosing 1 of 4 different Screen Modes.

private void Update()
{
    if (Input.GetKeyDown(KeyCode.Alpha1))
    {
        Screen.fullScreenMode = FullScreenMode.ExclusiveFullScreen;
    }
    else if (Input.GetKeyDown(KeyCode.Alpha2))
    {
        Screen.fullScreenMode = FullScreenMode.FullScreenWindow;
    }
    else if (Input.GetKeyDown(KeyCode.Alpha3))
    {
        Screen.fullScreenMode = FullScreenMode.MaximizedWindow;
    }
    else if (Input.GetKeyDown(KeyCode.Alpha4))
    {
        Screen.fullScreenMode = FullScreenMode.Windowed;
    }
}

If you test the above code out, you will see Unity go between 4 different Screen Modes:

  • Exclusive Full Screen (Unity changes the monitor to match the game, and minimizes the game if you try and do something outside of the game)
  • Full Screen Window (Unity stretches the game to fit the screen)
  • Maximized Window (Unity creates a maximized window – sometimes you may not notice a difference between this and the above two)
  • Windowed (Unity creates a standard, non-fullscreen window)

The Unity documentation on Screen.fullScreenMode is pretty bare bones however there is useful documentation on Screen and FullScreenMode.

You can also create a variable to store the full screen value like so:

    FullScreenMode fullScreenMode = FullScreenMode.ExclusiveFullScreen;
    Screen.fullScreenMode = fullScreenMode;

It works, however it does not set the resolution, which you may want to do.

Method 3

This method changes the full screen mode, resolution and set the target frame rate all at once:

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            FullScreenMode fullScreenMode = FullScreenMode.ExclusiveFullScreen;
            Screen.fullScreenMode = fullScreenMode;

            Resolution currentResolution = Screen.currentResolution;
            Screen.SetResolution(currentResolution.width, currentResolution.height, fullScreenMode, 60);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            Screen.SetResolution(960, 540, false, 60);
        }

So here we can change between Exclusive Full Screen and a window set to 960 x 540.

First we store the type of FullScreenMode we wish to use in a variable, fullScreenMode.

Next we use Screen.fullscreenMode to set the screen to Exclusive Full Screen.

The variable Resolution will store the resolution we want. In this case, we set it to Screen.currentResolution which gets the resolution of your screen.

We then use Screen.SetResolution() to set the resolution to the width and height of our screen, which is important for full screen. If you don’t do this, and the player was in Windowed Mode at a different resolution, then the screen could end up blurred or distorted because it will simply stretch the lower resolution image to fit the screen.

We then set the FullScreenMode again to whichever mode we want, in this case Exclusive Full Screen or Windowed for this example. We set the FullScreenMode twice because if we don’t, bad things happen.

If the player uses a small window, then switches to full screen, weird stuff can happen if you don’t follow my method, especially if there are dual monitors. You can get a blurred image, or worse, a ghost image can become plastered over the extra monitor, like the example below.

Above left is the game running in HD on my primary monitor. Above right is a ghost image plastered over my dual monitor (they’re different resolutions). This happened when I switch from a Window to Full Screen without first setting Screen.fullScreenMode.

Think of it as telling the PC, we want the game to run in Full Screen, then change the screen resolution and continue to run in Full Screen.

Finally 60 refers to the fps or frames per second. 60 is standard, and for a puzzle game like mine that’s fine. If you have a fast action game, you may want to lock it to the screens refresh rate, which is effectively what Vsync does for you. Instead of 60 you could use this:

Screen.SetResolution(960, 540, FullScreenMode.Windowed, Screen.currentResolution.refreshRate);

Screen.currentResolution.refreshRate gives you the refresh rate of your screen, effectively the same as switching on VSync. You can however switch on VSync manually a different way, which I mention lower down.

If you wish to make your Windowed Mode not resizable, go to “Project Settings > Player > Standalone Player Options > Resizable Window” and you can turn it on or off.

Difference Between Screen Modes

Visually you will notice very little difference between Exclusive Full Screen, Full Screen Window and Maximized Window. However, there are a few things to note:

  1. In Exclusive Full Screen, if the player has multiple screens and tries to click on one of the other screens, the game will be minimized. Therefore, some people may prefer Full Screen Window so that they can browse the web or do something else while gaming. It’s not something I usually do unless I’m playing an MMO, but it’s a feature many players requested for my games.
  2. Some people with old computers prefer running the game in a small window at low resolution because they claim it runs better in some cases. So options for a Windowed Mode that’s not full screen is useful to some players.
  3. Some players who only have one screen, like a lap top, also prefer to have the option for a small window so they can multi-task, another feature I was often requested in my games.
  4. Some people find that games run better for them in Exclusive Full Screen, so it’s worth having – so long as you make sure that the game is running at the same resolution as the desktop, as shown in Method 3 above.

There may be other differences I’m not aware of but those are the main ones I remember. Bottom line, the 3 windowed modes I was requested over and over again were:

  • Exclusive Full Screen
  • Full Screen Window
  • Windowed (a smaller window that may or may not be resizable)

I have no idea what the benefits of Maximized Window are, it seems the same as Full Screen Window to me. For Puzzledorf, I used Full Screen Window to allow users with more than one screen to multi-task and frame rate is no issue. For a more frame-rate intense heavy game, you might offer several full screen options.

Frame Rate

Frame rate, also known as fps, can be set separately in Unity. This is how:

int frameRateCap = 60;

void Start()
{
    Application.targetFrameRate = frameRateCap;

    // 1 = match monitor refresh rate. 0 = Don't use vsync: use targetFrameRate instead.
    QualitySettings.vSyncCount = 0;   
}

targetFrameRate will only work if vSyncCount is set to 0.

Application.targetFramerRate will try and run the game at the frame rate you set, but it won’t work if the platform doesn’t allow it or the device is too slow.

If you set it to -1, the game will run as fast as the platform allows, which in some cases may not be a good idea if there is no frame limit which can really stress the fans on some devices.

In Puzzledorf, I just set the Full Screen Mode and frame rate all in one line as in Method 3 as outlined under the Full Screen topic and lock. I use 60 fps and don’t set the frame rate separately.

VSync

VSync is a feature that syncs the games frame rate to the refresh rate of the monitor, traditionally 60hz which would be 60fps. It is sometimes used to help stop graphical artefacts such as tearing, where strange lines appear across the screen.

As mentioned above, targetFrameRate will only be used by Unity if vSyncCount is set to 0, meaning VSync is off. However, if you want VSync on, do the following:

    QualitySettings.vSyncCount = 1;   

1 will try and sync the games frame rate to the monitors refresh rate. You can also try:

    QualitySettings.vSyncCount = 2;   

2 will try and set the games frame rate to half of the monitors refresh rate. Bear in mind, this will be limited by what the platform allows in terms of frame rate and how slow the device is.

If vSyncCount is greater than 0, Unity will ignore targetFrameRate.

VSync is more important for fast-paced action games. If you notice visual tearing or weird glitches, or your play testers do, you might need VSync.

In games like first person shooters where the first one to fire has an advantage, higher frames can mean more responsive movement and action time, therefore potentially giving the player advantage. You need to do a lot of play testing and carefully consider your type of game and your market to know if VSync is important to your project.

In Puzzledorf, since it is turn based and frame rate is not an issue, I don’t use VSync and just set the frame rate to 60 fps at the same time as I set the Full Screen Mode, as outlined in Method 3 under the Full Screen topic.

If you enjoyed this article, try my game Puzzledorf out on Steam and Humble Bundle.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: