- Home /
Possible to change display at runtime?
Hey,
So I've been implementing my own custom graphics options that can be altered at runtime. While most of these options are easily changed, one I can't figure out how to do is change the display (for fullscreen purposes).
Basically I need to do what the launcher does: Be able to select which display to use and have the list of available resolutions update to match whatever display you have chosen, all while the game is running. I found Display.displays, which gives you access to all connected displays, but I don't know how to actually change which display the game pulls the supported resolutions from and uses for fullscreen.
Thanks!
Answer by crazymonkay · Mar 05, 2016 at 07:02 PM
Welp after all this time the answer was very simple:
PlayerPrefs.SetInt("UnitySelectMonitor", index); // Select monitor at index
This will automatically update the display the game is on and will update the Screen.resolutions array. You can use the Display.displays array to keep index within the appropriate bounds.
Edit: It seems that it is actually somewhat inconsistent as to whether or not it will change the game to the correct display, especially when changing Screen.fullscreen at the same time.
Edit 2: So changing the display the way mentioned above is very finicky. It also seems that the build will actually fullscreen to whichever display it is currently on, and the Screen.resolutions will actually update to match that display.
It took me a lot of testing to get this to work, but eventually I did find a bit of a nonsensical hack that allows me to consistently move the game to another display at runtime.
While I was running fullscreen at max resolution, the game would never switch to another monitor. However, if I changed the game resolution to anything lower than the native resolution, it would work every single time.
private IEnumerator TargetDisplayHack(int targetDisplay)
{
// Get the current screen resolution.
int screenWidth = Screen.width;
int screenHeight = Screen.height;
// Set the target display and a low resolution.
PlayerPrefs.SetInt("UnitySelect$$anonymous$$onitor", targetDisplay);
Screen.SetResolution(800, 600, Screen.fullScreen);
// Wait a frame.
yield return null;
// Restore resolution.
Screen.SetResolution(screenWidth, screenHeight, Screen.fullScreen);
}
I can't explain why it works, but I hope someone might be able to find this helpful if they're pulling their hair out trying to figure this out.
Hey, did you find the solution for a game not properly switching to another screen in the windowed mode and with the max possbile resolution? I have the same problem and I have no idea have to solve it.
Sorry. I don't have any improvement to what I have there. It's not great, but it works so I'm fine with it.
Thanks so much probably saved me 100s of hours.
Thanks for the solution, it helped greatly. I was trying to do some similar configuration via Command-Line Interface arguments at game launch, and nothing worked before that. Concerning application launch time, the trick works when called from the "Start" method in the Unity lifecycle, but not with "Awake". It seems the game engine always loads the monitor/resolution from the previous execution in between, and your Coroutine helps, especially with the "Wait a frame" part. Hope it helps too !
Your answer
Follow this Question
Related Questions
How to change the display at runtime. 1 Answer
Is it possible to switch monitor on a Mac? 0 Answers
UGUI Scaling across multiple monitors 0 Answers
Mutli Display doesnt update? 0 Answers
My multi-monitor support is broken. How can I fix this? 2 Answers