2
\$\begingroup\$

It seems to me that this behavior is incorrect (or at least illogical). Correct me if I'm wrong.

Repro (Unity 2020.3.26f1 LTS / default project settings):

  • Run the build (in fullscreen by default): enter image description here

  • Go to windowed mode (Alt + Enter): enter image description here

  • Resize the window with the mouse so that its resolution is reduced (to 500x500 pixels): enter image description here

  • Go back to fullscreen mode (Alt + Enter): enter image description here

Actual results:

  • When going back to fullscreen mode resolution is being kept 500x500 pixels (the scene is rendered in 500x500 pixels, the text is blurry and the canvas is squared).
  • Both Screen.currentResolution & Screen.width/height have 500x500 values.

Expected results:

  • Fullscreen-mode should restore the game resolution back to 1920x1080 (as on the 1st screenshot before going to windowed-mode).
  • Both Screen.currentResolution & Screen.width/height should have the values of 1920x1080.

The problem caused by this behavior is that the user cannot return to full-screen mode with full display resolution after switching to windowed mode and changing the window size.


Component script used in this test:

public class ResolutionInfo : MonoBehaviour
{
    public Text text;

    private Vector2 _resolution, _currentResolution;
    private bool _fullscreen;

    void Update()
    {
        Vector2 resolution = new Vector2(Screen.width, Screen.height);
        Vector2 currentResolution = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);

        if (_resolution == resolution && _currentResolution == currentResolution && _fullscreen == Screen.fullScreen)
            return;

        _resolution = resolution;
        _currentResolution = currentResolution;
        _fullscreen = Screen.fullScreen;

        text.text =
            $"FullScreen: {Screen.fullScreen}\r\n" +
            $"Screen.currentResolution = {Screen.currentResolution}\r\n" +
            $"Screen.width/height = {Screen.width}x{Screen.height}";
    }
}

P.S. Screen.resolutions list added to the request from Arian Keshvari comment: enter image description here enter image description here

\$\endgroup\$
8
  • \$\begingroup\$ I'm not sure if this will help you or not but use Screen.SetResolution(int width, int height, true); right when fullscreen is enabled. and to find the values of width and height just use Screen.resolutions and find the biggest supported size \$\endgroup\$
    – Arian_ki
    Mar 12, 2022 at 20:24
  • \$\begingroup\$ Screen.resolutions values are also incorrect after returning from Windowed mode (on the last screenshot). \$\endgroup\$
    – Serg
    Mar 13, 2022 at 8:23
  • \$\begingroup\$ screen.resolutions return a list of all supported screen sizes, can you please tell us what are the values in this list? I'm sure one of them is 1920.1080 \$\endgroup\$
    – Arian_ki
    Mar 13, 2022 at 9:22
  • \$\begingroup\$ Yes, I edited my question to add a couple of screenshots there. There are a lot of them there, how to guess which one is correct for the user's monitor (1920x1080 is just my case). \$\endgroup\$
    – Serg
    Mar 13, 2022 at 11:49
  • \$\begingroup\$ I think I've come up with a way to correctly determine the resolution of the user's current monitor. ...... var display = Display.displays[Camera.main.targetDisplay]; Screen.SetResolution(display.systemWidth, display.systemHeight, true); \$\endgroup\$
    – Serg
    Mar 13, 2022 at 11:52

1 Answer 1

2
\$\begingroup\$

This is a bit late, but I ran into the same problem recently and plopped this script together after exploring the forums a bit. It should do the trick once attached to any component in your scene. Hope that helps and good luck!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ManageFullscreenSwitch : MonoBehaviour
{
    private int _fullscreenWidth = 0;
    private int _fullscreenHeight = 0;
    private int _fullscreenAspectRatio = 0;

    private bool _fullscreen = false;

    private void Start()
    {
        _fullscreen = Screen.fullScreen;
        SetFullScreenValues();
    }
    private void Update()
    {
        if (_fullscreen != Screen.fullScreen)
        {
            if (Screen.fullScreen)
            {
                RestoreFullscreenResolution();
            }

            _fullscreen = Screen.fullScreen;
        }
    }

    private void RestoreFullscreenResolution()
    {
        Screen.SetResolution(_fullscreenWidth, _fullscreenHeight, true, _fullscreenAspectRatio);
    }

    private void SetFullScreenValues()
    {
        // Set the screen width and height
        int systemWidth = Display.main.systemWidth;
        int systemHeight = Display.main.systemHeight;

        // Get a list of all supported resolutions
        Resolution[] supportedResolutions = Screen.resolutions;

        // Find the closest supported resolution to the native resolution
        Resolution closestResolution = supportedResolutions[0];
        int smallestGapInResolution = int.MaxValue;

        foreach (Resolution resolution in supportedResolutions)
        {
            int gap = Mathf.Abs(resolution.width - systemWidth) + Mathf.Abs(resolution.height - systemHeight);

            if (gap < smallestGapInResolution)
            {
                smallestGapInResolution = gap;
                closestResolution = resolution;
            }
        }

        _fullscreenWidth = closestResolution.width;
        _fullscreenHeight = closestResolution.height;
        _fullscreenAspectRatio = _fullscreenWidth / _fullscreenHeight;
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .