Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Default Screen Dimensions Being Ignored After Build

Discussion in 'Scripting' started by AV_Corey, Oct 14, 2017.

  1. AV_Corey

    AV_Corey

    Joined:
    Jan 7, 2016
    Posts:
    113
    In my PlayerSettings I have the Default Screen Width set to 1280 and the Default Screen height set to 720. When I build the project the player launches in 1920x1080 and sets the width/height registry value to 1920x1080.

    This was working all fine and dandy before updating to Unity 2017.2 (wow!).

    It's also worth nothing that my builds are also creating a 22MB 'UnityPlayer.dll' file on my Desktop which was not happening before updating to 2017.2 and I'm thinking this may have something to do with it.

    Any suggestions?
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    5,942
    The build/player stores the last used screen size in the Windows Registry and reads/restores it when the game starts. Once these values are written to the Windows Registry, "Default Screen Width/Height" have no effect anymore, because Unity reads it from the Registry instead.

    To test modified Default Screen settings, it seems you need to remove the Windows Registry key before starting the game. That is at least the only workaround I found to make that work.

    These keys are located under:
    Computer\HKEY_CURRENT_USER\Software\<Company Name>\<Product Name>\

    Where <Company Name> and <Product Name> must be replaced with whatever you entered for those in the PlayerSettings.

    The keys you want to remove are:
    • Screenmanager Resolution Height
    • Screenmanager Resolution Width
    • Screenmanager Is Fullscreen mode
     
  3. RelentlessMetronome

    RelentlessMetronome

    Joined:
    Jan 8, 2013
    Posts:
    3
    THANK YOU, THANK YOU, THANK YOU, THANK YOU!!!
     
    Peter77 likes this.
  4. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,183
    Hi,

    Thanks for the tip. The problem is still present in 2019.1.0f2.

    I didn't delete the values you mentioned, I just modified the resolutions Height and Width with the correct values and my game displays at it should now. :)
     
    Peter77 likes this.
  5. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,183
    Has this been reported?
     
  6. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    5,942
    I haven't reported it.
     
  7. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    8,739
    This is kind of by design. You want the game to remember the last resolution it was run in. The player setting only specifies the default resolution - the resolution the game will start in. When you play any game, you usually want to adjust that resolution to either match your monitor resolution or reduce it if your GPU is weak. It would suck if you had to do it every time you started the game on your PC.
     
  8. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,183
    You're right, I want my game to play full screen 600 x 900 like I built if, not full screen native resolution. Right now it doesn't because the last resolution stored is 1920 x 1080 and setting the new resolution 900 x 600 is not recorded. If you find this normal, okay, I guess I'll have to do with it.
     
    mateov951 likes this.
  9. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    8,739
    You can always change the resolution to the one you want at startup in your code if you wish. However keep in mind that forcing a specific resolution will make your game look really badly on monitors with a different aspect ratio.
     
  10. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    5,942
  11. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,183
    Anyway, the fact that the registry key is never modified by the new resolution you want to choose for your game is an anomaly that needs to be fixed.

    Unfortunately, I suspect that reporting that as a bug will be useless and nobody will listen.

    How do I change the resolution at startup in code? Thank you.
     
    Last edited: Apr 26, 2019
  12. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    281
    I think I found that in a blog post when I googled around for letterboxing. Attach this script to your camera:

    Code (CSharp):
    1.  void Start()
    2.     {
    3.         float targetaspect = 16.0f / 9.0f;
    4.         float windowaspect = (float)Screen.width / (float)Screen.height;
    5.         float scaleheight = windowaspect / targetaspect;
    6.  
    7.         Camera camera = GetComponent<Camera>();
    8.  
    9.         // if scaled height is less than current height, add letterbox
    10.         if (scaleheight < 1.0f)
    11.         {
    12.             Rect rect = camera.rect;
    13.             rect.width = 1.0f;
    14.             rect.height = scaleheight;
    15.             rect.x = 0;
    16.             rect.y = (1.0f - scaleheight) / 2.0f;
    17.             camera.rect = rect;
    18.         }
    19.         else
    20.         { // else add pillarbox
    21.             float scalewidth = 1.0f / scaleheight;
    22.             Rect rect = camera.rect;
    23.             rect.width = scalewidth;
    24.             rect.height = 1.0f;
    25.             rect.x = (1.0f - scalewidth) / 2.0f;
    26.             rect.y = 0;
    27.             camera.rect = rect;
    28.         }
    29.     }


    Depending on the type of canvas you use, you might have to do some scaling there as well.
     
    APSchmidt likes this.
  13. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,183
    Thank you! I'm going to try it. :)

    Edit: I just tried it in a game that doesn't require pillarboxes just to see how it works and it does work very well. No need to modify the resolution in the Player settings. New aspect ratio 1:1.

    I owe you! :)

    Sans-titre-1.jpg
     
    Last edited: Apr 26, 2019
  14. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    8,739
    Well, as I said, that player setting denotes the default resolution. It's not "this is the resolution the game must run in" setting.

    While the editor could modify the registry for your current PC, it falls apart as soon as you copy your game to another machine.

    It does, but what kind of gamer wants his monitor space unused? Imagine you force 4:3 aspect ratio. People with ultra wide monitors will have over 40% of their monitor real estate rendered useless.
     
  15. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,183
    Hi again, :)

    I slightly modified the script so that the ratio can be chosen in the Inspector. Here is the result:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameRatioAspect : MonoBehaviour
    6. {
    7.     //ratioAspect: 1 = 1.0 / 1.0 ; 1.77 = 16.0 / 9.00 ; 1.33 = 4.0 / 3.0 and so on
    8.     public float ratioAspect;
    9.  
    10.     private void Start()
    11.     {
    12.         float targetaspect = ratioAspect;
    13.         float windowaspect = (float)Screen.width / (float)Screen.height;
    14.         float scaleheight = windowaspect / targetaspect;
    15.  
    16.         Camera camera = GetComponent<Camera>();
    17.  
    18.         // if scaled height is less than current height, add letterbox
    19.         if (scaleheight < 1.0f)
    20.         {
    21.             Rect rect = camera.rect;
    22.             rect.width = 1.0f;
    23.             rect.height = scaleheight;
    24.             rect.x = 0;
    25.             rect.y = (1.0f - scaleheight) / 2.0f;
    26.             camera.rect = rect;
    27.         }
    28.         else
    29.         { // else add pillarbox
    30.             float scalewidth = 1.0f / scaleheight;
    31.             Rect rect = camera.rect;
    32.             rect.width = scalewidth;
    33.             rect.height = 1.0f;
    34.             rect.x = (1.0f - scalewidth) / 2.0f;
    35.             rect.y = 0;
    36.             camera.rect = rect;
    37.         }
    38.     }
    39. }
    People who have extra wide monitors do not play games that require square or vertical game areas. :)
     
  16. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    8,739
    That's a dangerous assumption to make. When I buy a monitor, I definitely don't think which games I'll want to play for the next 5 years.
     
  17. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    281
    Peter77 likes this.
  18. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,183
    Where are we with this?
     
  19. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    5,942
    If you need this to test your local builds, just implement a PostProcessBuild step where you delete the registry keys. The next time you start your game, it will use the defaults.
     
  20. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    8,739
    This was addressed in 2020.1. If you change a default setting in the ediitor, the first time you launch the build it will force those settings.
     
    epicdustgame21, Shack_Man and Peter77 like this.
  21. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,183
    Ah, I need to test that, thank you. :)
     
  22. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    8,739
    I'm sorry, what's the issue here? The player setting is set to start in fullscreen. And the game starts in fullscreen.
     
  23. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    8,739
    What resolution is it running at? Kinda hard to tell from the video. Also, the video is 16:9 and the game seems to take full screen so at least the aspect ratio is correct.
     
  24. Juandapp

    Juandapp

    Joined:
    May 11, 2016
    Posts:
    45
    After reading everything, I see no solution, I want my game to run at 1280x720 even if my monitor is large. What should be done?
     
  25. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    8,739
    Screen.SetResolution(1280, 720, ...). I highly advice against it because you will make players running high resolution and/or different aspect ratio displays very, very upset.
     
unityunity