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.
Answer by Danielfc · Apr 08, 2019 at 11:22 AM
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.
Answer by SweatyChair · Aug 01, 2019 at 06:01 AM
This is our solution:
Build the game default fullscreen mode
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
}
}
Your answer
Follow this Question
Related Questions
unity3d 5 game on fullscreen shows only half of game screen 4 Answers
No readable font with unity 5.3.0 on OSX 10.9.5 6 Answers
Mac : Allow Fullscreen Switch but without command+F 0 Answers
Play sound within limited region 2 Answers
help me please. NullReferenceException: Object reference not set to an instance of an object 0 Answers