Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Robustly switching resolutions going from windowed to fullscreen and back?

Discussion in 'Scripting' started by Bas-Smit, Jul 18, 2020.

  1. Bas-Smit

    Bas-Smit

    Joined:
    Dec 23, 2012
    Posts:
    272
    For my windows standalone project I selected Fullscreen mode: Windowed @ 1024, 768 and ticked Allow Fullscreen Switch in player settings. To my surprise the resolution does not automatically get adjusted when switching to and from full screen mode (unity is this a bug?). I now manually adjust the resolution which sort of works, but Im not sure Im always getting the correct resolution (ie native for the appropriate monitor), especially when multiple monitors are involved. Has anyone found a better way than the code below?

    Code (CSharp):
    1. // I call this from Start, and from Update when _previousFullscreen != Screen.fullScreen
    2. void AdjustResolution()
    3. {
    4.     var r = Screen.resolutions.Last();
    5.     _previousFullscreen = Screen.fullScreen;
    6.  
    7.     if (Screen.fullScreen)
    8.         Screen.SetResolution(r.width, r.height, true, r.refreshRate);
    9.     else
    10.         Screen.SetResolution(1024, 768, false); // use preferred resolution
    11. }
    Another issue is when I restart the app I want a fresh start, not to have a previous resolution applied. Can this be fixed in unity or is this a windows issue?

    Cheers, Bas
     
  2. bentontr

    bentontr

    Joined:
    Jun 9, 2017
    Posts:
    39
    Bas-Smit likes this.