- Home /
Switch Monitor during Runtime
The launcher for a standalone Unity game is able to let the player choose which monitor the game is displayed on. Is there a way I can let the player do this in-game while the game is in fullscreen?
Answer by RuFrozen · Mar 10, 2015 at 01:31 PM
I know voodoo magic:
PlayerPrefs.SetInt("UnitySelectMonitor", 0); // Select monitor 1
PlayerPrefs.SetInt("UnitySelectMonitor", 1); // Select monitor 2
Also you need change resolution to appy monitor switch.
Anyway after restart it will select other monitor.
Confirmed the above worked - but is it possible to query the number of available monitors?
And, more importantly, is it possible to get the full list of resolutions for the current monitor, not just a monitor? Screen.resolutions sometimes returns values for another (the first indexed, but not primary) monitor.
Is there any way to open the game on the desired monitor from the get go? I.e. without having to open the game first, close it, then open it again.
Answer by lodendsg · Feb 01, 2016 at 05:43 PM
Ran into this one my self and found this post first then stumbled upon the following in the Unity docs
http://docs.unity3d.com/ScriptReference/Display.Activate.html
Display.Activate(); seems like the ideal option, the Display class contains additional handy members such as displays which lists the connected displays e.g. a means to get at the resolution.
Can't comment directly on @lodendsg's answer, but Display doesn't seem to really work for this (at least in any way I could tell). Calling Activate() creates another window (leading to two) rather than switching monitors, as it's designed for multiple displays at once, rather than switching.
To anyone wanting to get the native resolution of the specific monitor you're switching to rather than the generalized Screen.resolutions (as @superdupergc requested), you can actually use the Display.displays info for this. How I implemented it:
public void Set$$anonymous$$onitorIndex (int new$$anonymous$$onitorIndex)
{
PlayerPrefs.SetInt("UnitySelect$$anonymous$$onitor", new$$anonymous$$onitorIndex);
var display = Display.displays[new$$anonymous$$onitorIndex];
int resWidth = display.systemWidth;
int resHeight = display.systemHeight;
Screen.SetResolution(resWidth, resHeight, Screen.fullScreen);
}
hey how can you Screen.SetResolution the 2nd monitor?