Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Black bars on screen

Discussion in 'Scripting' started by Duende, Mar 4, 2019.

  1. Duende

    Duende

    Joined:
    Oct 11, 2014
    Posts:
    116
    Hi, how can I remove the black bars that appear in some resolutions? For example, resolution 1024x768:

    upload_2019-3-5_0-34-26.png

    1280x720:

    upload_2019-3-5_0-34-41.png

    1600x1024:

    upload_2019-3-5_0-37-26.png

    I have searched and try a lot of thing but I did not find a solution to this problem, can someone help me? What is missing me?
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    1,921
    If you tell Unity to display your game at a specific aspect ratio, but your game window is a different aspect ratio, Unity uses letterboxing to prevent your game from looking stretched. If you resize your game window to be the same aspect ratio as the resolution you are trying to display, it should go away. You can also set the game window to "Free Aspect", in which case it will change the game to match the window instead of the other way around.
     
  3. Duende

    Duende

    Joined:
    Oct 11, 2014
    Posts:
    116
    Thank you very much for answering. :)
    How do I do those two things? The "Free Aspect" you mean is in the "Game" window of the editor? Because these black bars appear in the builts, not in editor.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    1,921
    Oh, this is in the builds, not in the editor? Sorry.

    Are you running in fullscreen mode? Your monitor may not be capable of displaying the exact resolutions you are asking for.

    If they appear even in windowed mode, then I would think either they are actual objects in your game or there's something funky with your camera's settings.
     
  5. Duende

    Duende

    Joined:
    Oct 11, 2014
    Posts:
    116
    Yes, it's in fullscreen. I guess there are only 2 solutions to remove the black bars: prevent the user select resolutions that his monitor can not display, or stretch the camera so that the black bars are not shown, am I correct? Do you know how to do one of those two things or both?
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    1,921
    Hm. Are you selecting resolutions with Unity's built-in pop-up pre-game dialog, or with your own in-game options screen?

    If you are using your own options screen, you can (of course) filter the resolutions according to any criteria that you can implement in code. I would guess that the highest resolution in
    Screen.resolutions
    can probably be relied upon to match the monitor's natural aspect ratio; you could remove all resolutions with an aspect ratio that differ from that by more than (say) 5%.

    If you are using the built-in Unity dialog, then it's possible to constrain the selection to specific aspect ratios that you choose at build-time (e.g. if your game really doesn't work except in widescreen or something), but I don't know of a way to restrict it based on the hardware's aspect ratio. (Of course, you could write code to forcibly change it to something else as soon as the game launches, but that would presumably defeat the purpose of the dialog.)

    I don't think changing the camera will help you directly. If the monitor is expecting a certain resolution and is automatically letterboxing to that, then your game won't be able to send it any instructions to draw outside that area. You could hypothetically set the monitor to a different resolution and then modify your camera so that it renders in a lower resolution and stretches the result in order to make it look low-res, but that's probably not why you want to support lower resolutions.
     
  7. Duende

    Duende

    Joined:
    Oct 11, 2014
    Posts:
    116
    I have a menu where I let the user choose any resolution that his monitor / graphic card supports using Screen.resolutions, but how do I remove all resolutions with an aspect ratio that differ from monitor's natural aspect ratio by more than a percent?
     
  8. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    888
    Why do you want to limit the user? You should build some graphics around your gameview to let the resolution be whatever the user chooses but still to show just the content.
     
  9. Duende

    Duende

    Joined:
    Oct 11, 2014
    Posts:
    116
    Because I do not think a game with those black bars on the sides or up and down looks professional.

    That's why I wanted to stretch the camera to eliminate those bars, but it's true that it would give problems to detect coordinates on the screen for example.

    So the best solution is allow the user to only can choose resolutions where those black bars don't appear.
     
  10. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    888
  11. Duende

    Duende

    Joined:
    Oct 11, 2014
    Posts:
    116
    Those resolutions of the captures that I posted have been obtained with Screen.resolutions. :/

    That's why I ask this:
     
  12. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    888
    You can check rounded/percentage width/height and check the actual resolution, if it is not the same, dont add it to the selection.
     
  13. Duende

    Duende

    Joined:
    Oct 11, 2014
    Posts:
    116
    Please, could you be more specific or give an example? How do I know what the natural aspect ratio of the monitor is? Using the last element of the Screen.resolutions array, which is the maximum?
     
  14. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    888
    If you start the game first time without the unity option popup, it should be fullscreen, right? Take that screen.resolution
     
  15. Duende

    Duende

    Joined:
    Oct 11, 2014
    Posts:
    116
    Ok, I found a solution. For the people that come to this post looking for the same problem that I had, I have done this:

    1- Obtain the last resolution in the Screen.resolutions array.
    2- Divide width / height.
    3- That will give the aspect ratio.
    4- Do a For in the Screen.resolutions array looking for resolutions that, dividing its width between its height, give you an equal or similar value (a percentage) to the aspect ratio of the highest resolution.

    Example:

    1- Last resolution is 1680x1050.
    2- Divide 1680 / 1050.
    3- Aspect ratio is 1.6.
    4- Do a For and, for example, the resolution 1280x800 result in 1.6 that match with 1680x1050. Perhaps you also can keep with other resolutions as 1280x768 and 1600x1024 that give 1.6666667 and 1.5625, resulting in some small black bars.

    I have to try other computers, but right now it's the best solution I have.

    Thanks a lot for the help, guys.
     
    Last edited: Mar 7, 2019
    sder67 likes this.
  16. HonorableDaniel

    HonorableDaniel

    Joined:
    Feb 28, 2007
    Posts:
    2,952
    [Feature Request]: Please add a checkbox in the Player Settings useLetterboxing for Mac & PC standalone builds. Letterboxing is often not desired!
     
    Numa, Psyco92, Duende and 1 other person like this.
  17. Duende

    Duende

    Joined:
    Oct 11, 2014
    Posts:
    116
    Well, if in Edit / Project Settings / Player / Resolution / Fullscreen Mode you set as Exclusive Windows the black bars seem disappear. But this type of window is not fullscreen without borders (which is ideal for doing alt + tab).
     
  18. SweatyChair

    SweatyChair

    Joined:
    Feb 15, 2016
    Posts:
    124
    This is our solution:

    1. Build the game default fullscreen mode

    2. Use this script to:
    • When changing from fullscreen to window mode: set the width to 1920 and height proportional to monitor size ( Screen.currentResolution ), save the ratio in PlayerPrefs.

    • When changing from window to full-screen mode: set the width to 1920 and height proportional to the saved ratio.
    The reason we need PlayerPrefs to save the ratio is that Unity cannot obtain the monitor size in full-screen mode. Screen.currentResolution gives the current resolutions (the window size in window mode). There may be an even better solution using Windows.System.Display, but using PlayerPrefs is good enough. Let me know if anyone knows another way to do it.


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace SweatyChair
    4. {
    5.  
    6.     /// <summary>
    7.     /// Remove the letterbox when switching windows mode to full screen mode, set the windows to manual screen width
    8.     /// when switching full screen mode to windows mode. Standalone only.
    9.     /// </summary>
    10.     public class ScreenModeSwitcher : PersistentSingleton<ScreenModeSwitcher>
    11.     {
    12.  
    13. #if UNITY_STANDALONE
    14.  
    15.         private const string PREFS_LAST_SCREEN_RATIO = "LastScreenRatio";
    16.  
    17.         private bool _fullScreen;
    18.  
    19.         private void Start()
    20.         {
    21.             _fullScreen = Screen.fullScreen;
    22.             Screen.SetResolution(SettingSettings.current.manualScreenWidth, Mathf.RoundToInt(1f * SettingSettings.current.manualScreenWidth * Screen.currentResolution.height / Screen.currentResolution.width), Screen.fullScreen);
    23.         }
    24.  
    25.         private void Update()
    26.         {
    27.             if (_fullScreen != Screen.fullScreen) {
    28.                 if (_fullScreen) { // Full screen mode to window mode, simply set resolution with manualScreenWidth
    29.                     Screen.SetResolution(SettingSettings.current.manualScreenWidth, Mathf.RoundToInt(1f * SettingSettings.current.manualScreenWidth * Screen.currentResolution.height / Screen.currentResolution.width), Screen.fullScreen);
    30.                     PlayerPrefs.SetFloat(PREFS_LAST_SCREEN_RATIO, 1f * Screen.currentResolution.width / Screen.currentResolution.height); // Save the screen ratio, so this can be used next time full screen
    31.                 } else {
    32.                     // When in windows mode, both Screen.width/height or Screen.currentResolution are set to the windows size, try get one from saved PlayerPrefs value or default it to 16:9
    33.                     float screenRatio = PlayerPrefs.GetFloat(PREFS_LAST_SCREEN_RATIO, 16f / 9);
    34.                     Screen.SetResolution(SettingSettings.current.manualScreenWidth, Mathf.RoundToInt(1f * SettingSettings.current.manualScreenWidth / screenRatio), Screen.fullScreen);
    35.                 }
    36.                 _fullScreen = Screen.fullScreen;
    37.             }
    38.         }
    39.  
    40. #endif
    41.  
    42.     }
    43.  
    44. }
     
unityunity