Search Unity

(Solved) Screen.SetResolution() crashes our game.

Discussion in 'Windows' started by Alienpope, Apr 24, 2020.

  1. Alienpope

    Alienpope

    Joined:
    Jan 4, 2016
    Posts:
    20
    Using version 2019.3.10f1. I tried go back to earlier versions and found this problem persists there as well.

    Before I say anything else. I made a bug report in the in-editor bug reporter.

    But we're so close to releasing our game and then this bug surfaced.

    (width and height is taken from the user selected resolution. We've also tried changing these out to default numbers such as - 1920, 1080)
    We're simply calling - Screen.SetResolution(width, height, true); If we want fullscreen. Or change true to false if we want windowed. Depending on what the user has pre-selected in the display settings.

    We crash every single time this is being called. No matter if we go from windowed to fullscreen. Or fullscreen to windowed. Commenting out that line prevents the crash.

    We've also tried Screen.fullscreen. Same result.

    Is there a way around this? Can we switch between fullscreen and windowed in other ways?

    Edit: Solved! In my script currentResolutionOption wasn't initialized properly. Causing a resolution of 0x0 to be set. This crashes the unity player. So yes, my code caused the crash. But Tautvydas-Zilys also wanted me to make a bug report about the 0x0 resolution change causing a crash. Since this should never happen in the first place.

    Thanks Tautvydas-Zilys for helping!
     
    Last edited: Apr 29, 2020
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Which platform/gfx api are you targeting? Do you have any more details of the crash?
     
  3. Alienpope

    Alienpope

    Joined:
    Jan 4, 2016
    Posts:
    20
    PC, Mac, Linux standalone. I'm building on a PC. The gfx api is set to Auto Graphics for Windows. But im guessing that it builds for Dx11. And I'd love to give more details. But I'm not sure what other details i could give/would be useful.
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Can you attach the dump file Unity generates when the crash happens?
     
  5. Alienpope

    Alienpope

    Joined:
    Jan 4, 2016
    Posts:
    20
    I would but it won't accept me posting a .dmp file here "The uploaded file does not have an allowed extension.".

    Just so i won't break any rules, in what way can i upload it here? I could also refer to my bug report if you have access to those

    Edit: I can see that there's no way for me to publicly refer to my bug report. So, nvm with that haha.
     
    Last edited: Apr 27, 2020
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    You can post your 7-digit bug number. You can also put the .dmp file into a .zip and upload that.
     
  7. Alienpope

    Alienpope

    Joined:
    Jan 4, 2016
    Posts:
    20
    Ah gotcha! That's simple enough :) Here you go!
     

    Attached Files:

  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    The crash happens because the rendering thread gets fed bad data. However, since by default Unity rendering in multithreading, I cannot see what causes this. Could you run the game using "-force-gfx-direct" flag and upload another dump file? And can you also attach a player log?
     
  9. Alienpope

    Alienpope

    Joined:
    Jan 4, 2016
    Posts:
    20
    Put the dump file, player log and error log in there.

    I think I launched the game in single threaded rendering correctly. I'll try again if i failed to do so.

    Thanks for trying to help me so far! :)
     

    Attached Files:

  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Can you share the code that calls Screen.SetResolution? Are you by any chance setting the resolution to 0 x 0 (player log seems to suggest that)? It seems like we don't have any safeguards against it and Unity will just crash if you do that.
     
  11. Alienpope

    Alienpope

    Joined:
    Jan 4, 2016
    Posts:
    20
    Hm. That's very odd. I have a failsafe in case width or height are set at 0. It shouldn't in any case. But before i call Screen.SetResolution(width, height); I check if either of them or less than 100. If they are I set them to 1920x1080.

    I'll give you the code tomorrow once I'm back at work :)
     
  12. Alienpope

    Alienpope

    Joined:
    Jan 4, 2016
    Posts:
    20
    ResolutionManager handling resolution change and fullscreen change in the display settings menu

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using System.Linq;
    6.  
    7. public class ResolutionManager : MonoBehaviour
    8. {
    9.     public static ResolutionManager instance;
    10.  
    11.     public Resolution[] resolutions;
    12.  
    13.     public Resolution currentResolutionOption;
    14.  
    15.     public Resolution currentUserResolution;
    16.  
    17.     bool foundInitialRes;
    18.  
    19.     int count = 0;
    20.  
    21.     private void Awake()
    22.     {
    23.         instance = this;
    24.     }
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.         // Find what resolution the user currently has
    30.         currentUserResolution = FindInitialResolution();
    31.         GlobalSignals.OnSettingStartResolution.Trigger();
    32.     }
    33.  
    34.     private void Update()
    35.     {
    36.         if(!foundInitialRes)
    37.         {
    38.             currentUserResolution = FindInitialResolution();
    39.             GlobalSignals.OnSettingStartResolution.Trigger();
    40.             foundInitialRes = true;
    41.         }
    42.     }
    43.  
    44.     // Is called everytime user presses next res button
    45.     public void GoToNextResolution()
    46.     {
    47.         count++;
    48.  
    49.         if(count >= resolutions.Length)
    50.         {
    51.             currentResolutionOption = resolutions[0];
    52.             count = 0;
    53.         }
    54.         else
    55.         {
    56.             currentResolutionOption = resolutions[count];
    57.         }
    58.  
    59.         GlobalSignals.OnNextResolution.Trigger(currentResolutionOption);
    60.     }
    61.  
    62.     // Is called everytime user presses previous res button
    63.     public void GoToPreviousResolution()
    64.     {
    65.         count--;
    66.  
    67.         if(count < 0)
    68.         {
    69.             currentResolutionOption = resolutions[resolutions.Length - 1];
    70.             count = resolutions.Length;
    71.         }
    72.         else
    73.         {
    74.             currentResolutionOption = resolutions[count];
    75.         }
    76.  
    77.         GlobalSignals.OnNextResolution.Trigger(currentResolutionOption);
    78.     }
    79.  
    80.     // Is called when user press the apply settings button
    81.     public void ApplyResolution()
    82.     {
    83.         currentUserResolution = currentResolutionOption;
    84.         PlayerPrefs.SetInt("ScreenWidth", currentUserResolution.resolution.x);
    85.         PlayerPrefs.SetInt("ScreenHeight", currentUserResolution.resolution.y);
    86.         Screen.SetResolution(currentResolutionOption.resolution.x, currentResolutionOption.resolution.y, ResolutionFullscreenToggle.instance.ShouldBeFullscreen);
    87.         GlobalSignals.OnApplyResolution.Trigger();
    88.     }
    89.  
    90.     /// <summary>
    91.     /// Sets up starting resolution
    92.     /// </summary>
    93.     /// <returns></returns>
    94.     Resolution FindInitialResolution()
    95.     {
    96.         // First check for already saved resolution settings...
    97.         if(PlayerPrefs.HasKey("ScreenWidth") && PlayerPrefs.HasKey("ScreenHeight"))
    98.         {
    99.             Vector2Int savedRes = new Vector2Int(PlayerPrefs.GetInt("ScreenWidth"), PlayerPrefs.GetInt("ScreenHeight"));
    100.  
    101.             for (int i = 0; i < resolutions.Length; i++)
    102.             {
    103.                 if (resolutions[i].resolution.x == savedRes.x && resolutions[i].resolution.y == savedRes.y)
    104.                 {
    105.                     count = i;
    106.                     return resolutions[i];
    107.                 }
    108.             }
    109.         }
    110.  
    111.         // ... If we didn't find any get the screen resolution
    112.         Vector2Int newRes = new Vector2Int(Screen.width, Screen.height);
    113.  
    114.         // Fail safe
    115.         if(newRes.x <= 100 || newRes.y <= 100)
    116.         {
    117.             newRes = new Vector2Int(1920, 1080);
    118.         }
    119.  
    120.         for (int i = 0; i < resolutions.Length; i++)
    121.         {
    122.             if(resolutions[i].resolution.x == newRes.x && resolutions[i].resolution.y == newRes.y)
    123.             {
    124.                 count = i;
    125.                 PlayerPrefs.SetInt("ScreenWidth", resolutions[i].resolution.x);
    126.                 PlayerPrefs.SetInt("ScreenHeight", resolutions[i].resolution.y);
    127.                 return resolutions[i];
    128.             }
    129.         }
    130.  
    131.         return null;
    132.     }
    133.  
    134.     private void OnEnable()
    135.     {
    136.         GlobalSignals.OnSettingsApplied.Subscribe(ApplyResolution);
    137.     }
    138.  
    139.     private void OnDisable()
    140.     {
    141.         foundInitialRes = false;
    142.         GlobalSignals.OnSettingsApplied.Unsubscribe(ApplyResolution);
    143.     }
    144.  
    145. }
    146.  
    147. /// <summary>
    148. /// Class to store resolutions
    149. /// </summary>
    150. [Serializable]
    151. public class Resolution
    152. {
    153.     public string aspectRatio;
    154.     public Vector2Int resolution;
    155. }
    156.  
    GetSavedDisplaySettings: Is used in the very first scene of the game. It's used to get saved settings from previous game session

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. /// <summary>
    6. /// Checks for saved display settings and applies them at game start
    7. /// </summary>
    8. public class GetSavedDisplaySettings : MonoBehaviour
    9. {
    10.     void Start()
    11.     {
    12.         GetSettings();
    13.     }
    14.  
    15.     void GetSettings()
    16.     {
    17.         // If the user has already saved a V-sync option, we apply it
    18.         if(PlayerPrefs.HasKey("Vsync"))
    19.         {
    20.             // If the player enabled Vsync last session
    21.             if (PlayerPrefs.GetInt("Vsync") == 1)
    22.             {
    23.                 QualitySettings.vSyncCount = 1;
    24.             }
    25.             else // Else we apply the last saved fps limit
    26.             {
    27.                 if (PlayerPrefs.HasKey("FPSLimit"))
    28.                 {
    29.                     Application.targetFrameRate = PlayerPrefs.GetInt("FPSLimit");
    30.                 }
    31.                 QualitySettings.vSyncCount = 0;
    32.             }
    33.         }
    34.  
    35.         // If the user has saved a screen resolution, we apply it
    36.         if (PlayerPrefs.HasKey("ScreenWidth") && PlayerPrefs.HasKey("ScreenHeight") && PlayerPrefs.HasKey("Fullscreen"))
    37.         {
    38.             int width = PlayerPrefs.GetInt("ScreenWidth");
    39.             int height = PlayerPrefs.GetInt("ScreenHeight");
    40.  
    41.             int fullscreenInt = PlayerPrefs.GetInt("Fullscreen");
    42.             bool fullscreen = false;
    43.  
    44.             if(fullscreenInt == 1)
    45.             {
    46.                 fullscreen = true;
    47.             }
    48.             else
    49.             {
    50.                 fullscreen = false;
    51.             }
    52.  
    53.             // If width or height has invalid values
    54.             if(width <= 100 || height <= 100)
    55.             {
    56.                 width = 1920;
    57.                 height = 1080;
    58.             }
    59.  
    60.             if(fullscreen)
    61.             {
    62.                 Screen.SetResolution(width, height, true);
    63.             }
    64.             else
    65.             {
    66.                 Screen.SetResolution(width, height, false);
    67.             }
    68.         }
    69.         else if(PlayerPrefs.HasKey("ScreenWidth") && PlayerPrefs.HasKey("ScreenHeight"))
    70.         {
    71.             int width = PlayerPrefs.GetInt("ScreenWidth");
    72.             int height = PlayerPrefs.GetInt("ScreenHeight");
    73.  
    74.             if (width <= 100 || height <= 100)
    75.             {
    76.                 width = 1920;
    77.                 height = 1080;
    78.             }
    79.  
    80.             Screen.SetResolution(width, height, false);
    81.         }
    82.     }
    83. }
    84.  
     
  13. Alienpope

    Alienpope

    Joined:
    Jan 4, 2016
    Posts:
    20
    And here's a screenshot of all the res options we have in the resolutions array in the ResolutionManager

     
  14. Alienpope

    Alienpope

    Joined:
    Jan 4, 2016
    Posts:
    20
    Might be worth to note. Someone else on the team is playing/testing the game on a mac. He never gets any crashes
     
  15. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Would it be possible for you to send me a crashing game build and tell me how to trigger a crash? Unfortunately I don't currently understand how the game can get into this situation.

    Alternatively, you could also submit a bug report and we'll investigate it. Crashing like that is definitely a bug in Unity.
     
  16. Alienpope

    Alienpope

    Joined:
    Jan 4, 2016
    Posts:
    20
    Alright! Where can I send you a build?

    How I trigger the crash
    Launch the game
    Go into options
    Then display settings
    Then press the full screen toggle
    Hit apply

    Optional (won't cause a crash)
    Have fun with the game ^^
     
  17. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    You could upload it to some kind of cloud storage (google drive, onedrive, etc) and just PM me the link.
     
    Alienpope likes this.
  18. holyfot

    holyfot

    Joined:
    Dec 16, 2016
    Posts:
    42
    Me and many others have the same issue of the game freezing when you try to: Screen.SetResolution(x, y, FullScreenMode.ExclusiveFullScreen, refreshRate);

    If you just use true/false instead of FullScreenMode it doesn't freeze, the FullScreenMode seems to actually use the hardware to set the screen mode, while true/false is kind of a soft mode. This bug has been around for years and happens on all kinds of hardware, very easy to reproduce. Just tested it on 2019.4.3f1 LTS but its been around since 2017.
     
  19. zdolezal

    zdolezal

    Joined:
    Sep 27, 2017
    Posts:
    75
    If you use true, it's same as borderless window mode but not as "fullscreen" mode.
     
  20. flashmandv

    flashmandv

    Joined:
    Mar 26, 2015
    Posts:
    156
    Is there any solution to this? Our game freezes for 80% of our webgl players.
    Reproducible with Unity 2019.4.26 LTS on my Acer Swift SF314-56G
    On my other PC's it works. But many players are using older model laptops or lowend ones and it freezes every time without error logs.

    In our case if freezes even when passing directly true/false to full screen mode

    UPDATE: The game wasn't freezing. It was buttons hitarea misplacement due to Screen.SetResolution method calls. The only workaround to fix this was to stop using this method. Screen.fullScreen = true/false works though.

    I hope somebody notices and fixes this after all
     
    Last edited: Sep 6, 2021
  21. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    That looks entirely unrelated. Can you post on the WebGL forum about this issue? This subforum is for discussing Windows player issues.
     
  22. flashmandv

    flashmandv

    Joined:
    Mar 26, 2015
    Posts:
    156
    Thanks. I created a new thread there
     
  23. damian_pablo_gonzalez

    damian_pablo_gonzalez

    Joined:
    Jan 3, 2018
    Posts:
    15
    Maybe this is useful for someone: In my case (Unity 2021.1.19f1) Screen.SetResolution in editor made the game unresponsive, but standalone worked fine.