Hi, so I’ve been working on the menu for a game and I’m running into an issue where going windowed at the max resolution supported by the monitor leaves it at a different resolution, but my attempts to detect the lowered resolution don’t seem to work. In the video, my friend demonstrates going from 1920 x 1080 FullScreen Windowed to Windowed, which sets the actual size of the window to 1600 x 900.
I’ve attached the resolution/fullscreen portions of the script handling the menu. What’s interesting is on my machine, which is a laptop, the code leaves the windowed version at full resolution but partially off-screen since it is too big, so my code works at least some of the time.
If someone has an idea on how to fix this, please let me know, it’s been bothering me for weeks.
Unity personal version: 2020.3.37f1
We plan to upgrade to the last LTS release of 2020 when that comes, but we’ve stuck with other versions of 2020 for a while.
// Start is called before the first frame update
void Start()
{
if( !shouldIInit )
{
CloseAllMenus(closeAllMenusOnStart);
rePlayer = ReInput.players.GetPlayer(playerId);
gs = GameStates.Active;
playerObj = GameObject.FindGameObjectWithTag("Player");
if (playerObj != null)
{
pcontroller = playerObj.GetComponent<PlayerController_MainCharacter>();
}
//Fetch the current EventSystem. Make sure your Scene has one.
m_EventSystem = EventSystem.current;
lm = GameObject.FindObjectOfType<LoadManager>();
return;
}
lm = GameObject.FindObjectOfType<LoadManager>();
//Fetch the current EventSystem. Make sure your Scene has one.
m_EventSystem = EventSystem.current;
if (PlayerPrefs.GetInt("UnitySelectMonitor", 0) == -1)
{
PlayerPrefs.SetInt("UnitySelectMonitor", 0);
//monDropdown.value = 0;
//SetMonitor();
}
else
{
//monDropdown.value = PlayerPrefs.GetInt("UnitySelectMonitor", 0);
//SetMonitor();
}
[B]resolutions = Screen.resolutions;
//Debug.LogError(resolutions.Length);
//resDropdown.ClearOptions();
resDropdownTMPro.ClearOptions();
List<string> options = new List<string>();
int currentResolutionIndex = 0;
for(int i = 1; i < resolutions.Length; i++)
{
string option = resolutions[i].width + " x " + resolutions[i].height;
if (!options.Contains(option))
{
options.Add(option);
}
if(resolutions[i].width == Screen.width && resolutions[i].height == Screen.height)
{
currentResolutionIndex = i;
//Debug.LogError(i);
}
}
int curRes = PlayerPrefs.GetInt("CurrentResolution", resolutions.Length - 2);
/*
resDropdown.AddOptions(options);
resDropdown.value = curRes;
//resDropdown.value = currentResolutionIndex;
resDropdown.RefreshShownValue();
*/
resDropdownTMPro.AddOptions(options);
resDropdownTMPro.value = curRes;
resDropdownTMPro.RefreshShownValue();[/B]
[B] int selectedFullscreenMode = PlayerPrefs.GetInt("SelectedFullscreenMode", 0);
//fsDropdown.value = selectedFullscreenMode;
fsDropdownTMPro.value = selectedFullscreenMode;
SetResolution();[/B]
// Make the game run as fast as possible
Application.targetFrameRate = 300;
int savedVsync = PlayerPrefs.GetInt("EnableVsync", 1);
if(savedVsync == 1)
{
vsyncTog.isOn = true;
}
else
{
vsyncTog.isOn = false;
}
// Turn on/off v-sync
SetVsyncCount();
Debug.Log(Screen.currentResolution.refreshRate + " : refresh rate of monitor!");
//QualitySettings.vSyncCount = 0;
//Cursor.lockState = CursorLockMode.Locked;
rePlayer = ReInput.players.GetPlayer(playerId);
gs = GameStates.Active;
//Debug.Log("displays connected: " + Display.displays.Length);
// Display.displays[0] is the primary, default display and is always ON, so start at index 1.
// Check if additional displays are available and activate each.
//options.Clear();
//for (int i = 0; i < Display.displays.Length; i++)
//{
// //if (i > 0)
// //Display.displays[i].Activate();
// string option = ("Monitor " + (i + 1));
// if (!options.Contains(option))
// options.Add(option);
//}
//monDropdown.AddOptions(options);
//monDropdown.value = PlayerPrefs.GetInt("UnitySelectMonitor");
//monDropdown.RefreshShownValue();
//SetMonitor();
//load saved sfx/music volume
float storedMusic = Mathf.Clamp(PlayerPrefs.GetFloat("StoredMusicVolume", 0.5f), 0.0f, 1.0f);
musicSlider.value = storedMusic;
SetMusicVolume();
float storedSFX = Mathf.Clamp(PlayerPrefs.GetFloat("StoredSFXVolume", 0.5f), 0.0f, 1.0f);
sfxSlider.value = storedSFX;
SetSFXVolume();
if (myControlMapper == null)
{
myControlMapper = GameObject.FindGameObjectWithTag("RewiredControlMapper").GetComponent<ControlMapper>();
}
/*
string deviceID = SystemInfo.deviceModel;
if (deviceID == "iPhone10,3 " || deviceID == "iPhone10,6")
{
// This is an iPhoneX
//now change the graphics tier to tier 3
Graphics.activeTier = GraphicsTier.Tier3;
}
*/
if(Graphics.activeTier != GraphicsTier.Tier3)
{
Graphics.activeTier = GraphicsTier.Tier3;
}
CloseAllMenus(true);
if (parentMainMenuCanvas != null)
{
parentMainMenuCanvas.SetActive(true);
}
if (subMainMenuCanvas != null)
{
subMainMenuCanvas.SetActive(true);
}
}
[B]public IEnumerator SetResolution (bool callFSMode)
{
//Debug.LogError(resDropdown.value);
//Debug.LogError(resDropdownTMPro.value);
//Resolution res = resolutions[resDropdown.value + 1];
if (resDropdownTMPro != null && resolutions != null)
{
yield return new WaitForSeconds(0.5f);
Resolution res = Screen.currentResolution;
int currentResolutionIndex = 0;
for (int i = 1; i < resolutions.Length; i++)
{
//string option = resolutions[i].width + " x " + resolutions[i].height;
if (resolutions[i].width == Screen.width && resolutions[i].height == Screen.height)
{
currentResolutionIndex = i;
break;
//Debug.LogError(i);
}
}
resDropdownTMPro.value = currentResolutionIndex - 1;
//Screen.SetResolution(res.width, res.height, Screen.fullScreen);
if (res.width > 4096 || res.height > 3750)
{
myScroller.ResolutionChangeFunc(true);
}
else
{
myScroller.ResolutionChangeFunc(false);
}
if (callFSMode)
{
SetFullscreenMode();
}
//PlayerPrefs.SetInt("CurrentResolution", resDropdown.value);
resDropdownTMPro.RefreshShownValue();
PlayerPrefs.SetInt("CurrentResolution", resDropdownTMPro.value);
}
}
public void SetResolution()
{
//Debug.LogError(resDropdown.value);
//Debug.LogError(resDropdownTMPro.value);
//Resolution res = resolutions[resDropdown.value + 1];
if (resDropdownTMPro != null && resolutions != null)
{
Resolution res = resolutions[resDropdownTMPro.value + 1];
Screen.SetResolution(res.width, res.height, Screen.fullScreen);
if (res.width > 4096 || res.height > 3750)
{
myScroller.ResolutionChangeFunc(true);
}
else
{
myScroller.ResolutionChangeFunc(false);
}
//SetFullscreenMode();
//PlayerPrefs.SetInt("CurrentResolution", resDropdown.value);
PlayerPrefs.SetInt("CurrentResolution", resDropdownTMPro.value);
}
}
public void SetFullscreenMode()
{
//if(fsDropdown.options[fsDropdown.value].text == "Fullscreen Window")
if(fsDropdownTMPro.options[fsDropdownTMPro.value].text == "Fullscreen Window")
{
Screen.fullScreenMode = FullScreenMode.FullScreenWindow;
Debug.Log("Fullscreen Window");
Screen.fullScreen = true;
PlayerPrefs.SetInt("SelectedFullscreenMode", 0);
}
//else if(fsDropdown.options[fsDropdown.value].text == "Exclusive Fullscreen")
else if (fsDropdownTMPro.options[fsDropdownTMPro.value].text == "Exclusive Fullscreen")
{
Screen.fullScreenMode = FullScreenMode.ExclusiveFullScreen;
Debug.Log("Exclusive Fullscreen");
Screen.fullScreen = true;
PlayerPrefs.SetInt("SelectedFullscreenMode", 1);
}
//else if (fsDropdown.options[fsDropdown.value].text == "Maximized Window")
else if (fsDropdownTMPro.options[fsDropdownTMPro.value].text == "Maximized Window")
{
#if UNITY_STANDALONE_OSX
Screen.fullScreenMode = FullScreenMode.MaximizedWindow;
Debug.Log("Maximized Window");
//Screen.fullScreen = false;
PlayerPrefs.SetInt("SelectedFullscreenMode", 2);
#else
Debug.Log("Not on MAC/OSX build. Maximized Window menu option should do nothing.");
#endif
}
//else if (fsDropdown.options[fsDropdown.value].text == "Windowed")
else if (fsDropdownTMPro.options[fsDropdownTMPro.value].text == "Windowed")
{
Screen.fullScreenMode = FullScreenMode.Windowed;
Debug.Log("Windowed");
//Screen.fullScreen = false;
PlayerPrefs.SetInt("SelectedFullscreenMode", 3);
}
StartCoroutine(SetResolution(false));
}[/B]
Edit: Nvm, I changed my code to make sure windowed resolutions are stored separately and must be smaller than the biggest fullscreen resolution. Seems to work.