Avoiding The Black Bars (LetterBoxing)

We have upgraded our project from Unity 2017.4.4f1 to 2018.3.11f1,

When building for a Mac we notice a different result which we want to know how to avoid, we set our app to Windowed and it is opening in a windowed mode as required, but when we maximize the window (using the window maximize button) there are black bars on top and on the bottom which is also known as letterboxing and obviously not everyone would want their app to be in this style.

Would appreciate some information on why they appear while on 2017.4.4f1 it didn’t happen.

Except the removal of “Mac Fullscreen Mode” and “D3D11 Fullscreen Mode” in PlayerSettings → Resolution and Presentation window since they became deprecated nothing has changed in terms of any parameters that were set there.

Thank you in advance.

I have managed to solve my issue, i do not believe its the best solution out there but it does what i wanted it to do.

Basically everything related to the application resolution information is saved by PlayerPrefs with the keys such as :

  • Screenmanager Resolution Width
  • Screenmanager Resolution Height
  • Screenmanager Fullscreen mode
  • Screenmanager Resolution Use Native

By deleting all the keys of screenmanager when quitting the app i managed to give the feeling of every time the user opens the app it will act like it was opened for the first time when it comes to the resolution only.

Which means if somehow something happened with the resolution of the app and something got messed up then that information will not be saved and will be reset once the app is being closed and any other information that is not related to that in the PlayerPrefs will remain such as quality settings and more.

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.

using UnityEngine;

namespace SweatyChair
{

	/// <summary>
	/// Remove the letterbox when switching windows mode to full screen mode, set the windows to manual screen width
	/// when switching full screen mode to windows mode. Standalone only.
	/// </summary>
	public class ScreenModeSwitcher : PersistentSingleton<ScreenModeSwitcher>
	{

		#if UNITY_STANDALONE

		private const string PREFS_LAST_SCREEN_RATIO = "LastScreenRatio";

		private bool _fullScreen;

		private void Start()
		{
			_fullScreen = Screen.fullScreen;
		}

		private void Update()
		{
			if (_fullScreen != Screen.fullScreen) {
				if (_fullScreen) { // Full screen mode to window mode, simply set resolution with manualScreenWidth
					Screen.SetResolution(SettingSettings.current.manualScreenWidth, Mathf.RoundToInt(1f * SettingSettings.current.manualScreenWidth * Screen.currentResolution.height / Screen.currentResolution.width), Screen.fullScreen);
					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
				} else {
					// 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
					float screenRatio = PlayerPrefs.GetFloat(PREFS_LAST_SCREEN_RATIO, 16f / 9);
					Screen.SetResolution(SettingSettings.current.manualScreenWidth, Mathf.RoundToInt(1f * SettingSettings.current.manualScreenWidth / screenRatio), Screen.fullScreen);
				}
				_fullScreen = Screen.fullScreen;
			}
		}

		#endif

	}

}