Search Unity

Change Resolution in Build

Discussion in 'Scripting' started by GossamerGames, Aug 19, 2019.

  1. GossamerGames

    GossamerGames

    Joined:
    Jul 12, 2017
    Posts:
    30
    Hello, I am having issues changing the resolution of the game while playing, this used to work fine, and when I step through my code, it does reach the point at which it will set the resolution. I have code here to show how we are handling full screen and resolution setting.


    Code (CSharp):
    1. private void UpdateFullscreen(SettingKey key)
    2.     {
    3.         bool fullscreen = SaveManager.Instance.fullScreen;
    4.  
    5.         if (fullscreen)
    6.             Screen.fullScreenMode = FullScreenMode.ExclusiveFullScreen;
    7.         else
    8.             Screen.fullScreenMode = FullScreenMode.Windowed;
    9.  
    10.         Screen.fullScreen = fullscreen;
    11.     }
    12.  
    13.     private void UpdateResolution()
    14.     {
    15.  
    16.         int w = SaveManager.Instance.resolutionW;
    17.         int h = SaveManager.Instance.resolutionH;
    18.         Screen.SetResolution(w, h, Screen.fullScreen, Screen.currentResolution.refreshRate);
    19.     }
    What happens when I play is that the size of the window does not change at all.

    Thanks!
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    What resolution is it trying to set? Are the numbers reasonable? (e.g. more than 100 and less than your monitor's max resolution.) Are they different from the resolution the window had before your code ran? (It was my impression that Unity games normally remember these settings automatically.)

    When in your program is this happening? If you try to do it immediately on startup, I could imagine that might cause problems.
     
  3. GossamerGames

    GossamerGames

    Joined:
    Jul 12, 2017
    Posts:
    30
    • It could be any resolution that the monitor supports. For example, it will be running at 1920 x 1080 and I will attempt to change the resolution to 1280 x 720.
    • I believe that the numbers I am trying to set are reasonable. The way that it decides what possible resolutions to show is by generating a list at app startup to gather all the possible resolutions at their refresh rates. The code to gather this list is here.
      Code (CSharp):
      1. private void PopulateListValues()
      2.     {
      3.         // Get resolutions with one refresh rate
      4.         var culledResolutions = Screen.resolutions.Select(resolution => new Resolution { width = resolution.width, height = resolution.height }).Distinct();
      5.  
      6.         // Store the resolutions in an array
      7.         Resolution[] resolutions = culledResolutions.ToArray();
      8.         List<string> resStrings = new List<string>();
      9.  
      10.         for (int i = 0; i < resolutions.Length; i++)
      11.         {
      12.             resStrings.Add(
      13.                 ResolutionString(resolutions[i].width, resolutions[i].height)
      14.             );
      15.         }
      16.         _list.DisplayOpts = resStrings;
      17.     }
    • I am not entirely sure what you mean by "they" however, if I understand correctly. The resolution starts at the resolution of the screen.
    • I am calling the code to change the resolution from the settings window, this is far past startup.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    You said you stepped through your code in the debugger and verified that it got to the line where the resolution was actually being set. While you're there, you should also use the debugger to check the actual values of all relevant variables at that exact moment, rather than trying to calculate what they should be.

    Seeing the actual values will help you narrow down the range of possible problems and focus on the right part of your code (e.g. maybe you have an error in another part of your code that's feeding bad data into the part that's crashing, so the code where the crash occurs isn't actually at fault).
     
    SparrowGS likes this.
  5. GossamerGames

    GossamerGames

    Joined:
    Jul 12, 2017
    Posts:
    30
    While stepping through the code, the width and height values were correct according to what value I was selecting. The program never crashes, it just runs as normal, as if nothing at all was changed.

    Are there specific values I should be watching that are not the width and height?
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I'd look at all 4 of the parameters that you're passing to Screen.SetResolution just for completeness, but it's pretty unlikely that the other two would be invalid.

    Also, rather than just checking whether the program crashes, you should be checking whether an exception gets thrown. In Unity, an exception won't usually stop the whole program, but it will still abort the current function. If you "stepped over" Screen.SetResolution in the debugger and got to the next line, though, then there was no exception there.

    If your game is definitely executing the Screen.SetResolution call with no exceptions, the parameters are sane, and the window does not match those parameters at the end of the call, then we're into weirder options...
    • Is it possible that some other piece of code is also changing the resolution at the same time?
    • In Player Settings => Resolution and Presentation => Supported Aspect Ratios, have you checked the box corresponding to the resolution you are trying to set? (I'm not sure this would stop you even if you hadn't, but it might be worth checking)
    • Is this running from the main Unity thread? What event or Unity function was the root of this callstack?
    • You might check the values of Screen.width, Screen.height, and Screen.fullscreen after the call to Screen.SetResolution returns. Heck, you might check their values before you call Screen.SetResolution, too. Just see if anything seems weird.
     
    GossamerGames likes this.
  7. GossamerGames

    GossamerGames

    Joined:
    Jul 12, 2017
    Posts:
    30
    So I was able to step through the program, I checked the width and height values that were being passed in and all seems fine. I noticed in the Unity documentation that the screen resolution doesn't actually get set until the next frame, so I also had it call a Coroutine that would wait a frame and then output the screen resolution. The thing is, the resolution never actually changes. However, it was under my impression that this may be the case in Editor builds as the resolution setting might be weird. Is there a way I can step through this in a standalone build?

    As for your other questions, I don't believe it could be set anywhere else, would the best way of checking this be to search for the phrase Screen.SetResolution and put breakpoints at all the spots it is called?

    I have the supported aspect rations set that it should be 16:9 and 16:10. I believe that this is good and fine.

    I don't know which thread this is running in, how might I check that, I asume this is something I can only check in a build?

    As I mentioned before I checked these and everything goes into the function properly, and it steps over properly, nothing is thrown, but the resolution doesn't actually change.

    Any ideas?
     
  8. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Setting the resolution definitely won't work inside the Unity editor, since Unity will be controlling those settings through the game window.

    Yes, but you might have to fiddle with it for a while to get it to work. I haven't done it recently and I'm finding conflicting instructions online.

    The main steps, which you can find somewhere towards the middle of this Microsoft page, are:
    • When you make your build, make sure that "Development Build" and "Script Debugging" are checked in the build settings
    • After launching your build, go to Visual Studio, select Debug => Attach to Process... and select your game
    However, getting Visual Studio to properly load symbols so that you can set breakpoints appears to be black magic. There's some instructions from Unity that don't seem to work, a StackOverflow thread suggesting about 5 different solutions that I haven't tried, and some general troubleshooting suggestions from Microsoft.

    Anyone done this successfully recently?
     
    GossamerGames likes this.
  9. GossamerGames

    GossamerGames

    Joined:
    Jul 12, 2017
    Posts:
    30
    Hahaha wow, okay, well I will try my best to go through these steps and see if one works for me. But yes, I would love more input from others that have successfully debugged in a Windows Standalone Build.