Search Unity

Screen.SetResolution works but sometimes screen doesn't update

Discussion in 'Windows' started by djfrail, May 23, 2019.

  1. djfrail

    djfrail

    Joined:
    Jan 16, 2014
    Posts:
    116
    Hi - I'm using Screen.SetResolution() in my Windows 10 build to change resolution. In a co-routine, It changes the resolution, waits 0.5 secs, then re-loads the current scene. But sometimes after it's done, what you see on the screen doesn't get updated...until you move the window the game is running with the mouse, or switch to another application and back, or sometimes just moving the mouse.

    It usually works fine, but in maybe like 1 out of 50 times it'll have this problem. The resolution I change to/from doesn't seem to make a difference about when the problem will occur. I'm using Unity 5.6.6f2.

    Maybe this is just a Window 10 issue? Thanks for any help!
     
    Last edited: May 23, 2019
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,536
    Can you show a screenshot of what you're experiencing? Or perhaps a video? Are you targeting Windows Standalone or Universal Windows Platform?
     
  3. djfrail

    djfrail

    Joined:
    Jan 16, 2014
    Posts:
    116
    Targeting Windows Standalone. A screenshot wouldn't tell you much - it would just show a black screen. I have a dialog where you can select a resolution from the list of resolutions Screen.resolutions and an "apply" button that calls a coroutine DoResolutionChange(). I cover the screen with a giant black box at the start of DoResolutionChange() to hide the on-screen graphics distortion that happens when you change resolutions, do the resolution change and wait 0.5 secs, then load the Gameplay scene. When the problem happens, what I'm seeing is the black screen when I should be seeing the graphics from the start of my Gameplay scene.

    Also I'm using 2D Toolkit, which could be a factor.

    Here's the code for DoResolutionChange():

    Code (CSharp):
    1.  
    2.     public IEnumerator DoResolutionChange()
    3.     {
    4.         // Unhide giant black sprite that covers the screen, makes it a black screen
    5.         blackScreenCover.SetActive(true);
    6.  
    7.         // Do resolution change
    8.         Resolution aResolution = screenResolutionsList[screenResolutionsArrayIndex];
    9.  
    10.         bool doFullScreen = checkbox_Fullscreen.GetComponent<tk2dUIToggleControl>().IsOn;
    11.         Screen.SetResolution(aResolution.width, aResolution.height, doFullScreen);
    12.  
    13.         // Turn off any way to hit a button (click/touch/keyboard/gamecontroller)
    14.         // on the Options screen, temporarily
    15.         GlobalStuffManager.Inst.inputsEnabled = false;
    16.  
    17.         // Wait here for half a sec for Screen.SetResolution to do its thing...
    18.         // cuz some monitors can take a while to change rez
    19.         yield return new WaitForSeconds(0.5f);
    20.  
    21.  
    22.       //******** OK - from here on down, the game is at the new resolution *********
    23.  
    24.  
    25.         // Turn inputs back on
    26.         GlobalStuffManager.Inst.inputsEnabled = true;
    27.  
    28.         string tempLevelName = SceneManager.GetActiveScene().name;
    29.  
    30.         // Load an empty scene first to clear out old tk2d 1x2s4x textures.
    31.         SceneManager.LoadScene("EmptyScene");
    32.         GlobalStuffManager.Inst.SetTk2d_1X2X4X();
    33.  
    34.         // Restart Gameplay scene at the new resolution
    35.         if (tempLevelName == "Gameplay")
    36.         {
    37.            SceneManager.LoadScene("Gameplay");
    38.         }
    39.         // Restart MainMenu scene at the new resolution
    40.         else if (tempLevelName == "MainMenu")
    41.         {
    42.             SceneManager.LoadScene("MainMenu");
    43.         }
    44.         // Something went wrong...
    45.         else
    46.         {
    47.         }
    48. }
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,536
    Is this happening in windowed mode, fullscreen mode or both? Do you know if your other scene actually gets loaded (do your scripts execute?) or whether it stalls the loading until you move the window? Trying to figure out whether it's a graphical glitch or the whole engine hangs.
     
  5. djfrail

    djfrail

    Joined:
    Jan 16, 2014
    Posts:
    116
    Windowed mode. Haven't seen it happen in full screen mode yet, I don't believe. The window size actually changes size so the resolution change does happen... I guess I can't say if the scene loads or the engine hangs because if I go to check the log file then going away from the game can make the black screen go away before I can see the log file. I guess I could play some music when the Gameplay scene starts (and stop it before rez change) and can tell that way.
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,536
    It would be great if you could figure it out with music. That would determine followup diagnostic steps.
     
  7. djfrail

    djfrail

    Joined:
    Jan 16, 2014
    Posts:
    116
    Ah so I got it to do the black screen problem using a build that plays music when Gameplay starts - and when it did the black screen it did NOT play the music. Engine hang??
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,536