Display and Screen Class

Hello Everyone,

I have a problem with the

Display.displays

and

Screen.resoltions

Methods.

I am populating dropdowns with the options that these methods provide. That is working well when in the editor, but if I built the game I don’t get any displays and only one resolution.

Does anyone have any idea why?

Thanks in advance

Googling for those two ultra-common class names is going to be completely useless.

Instead, Google around for questions about “Unity not finding other displays” and “Unity not reporting all resolutions” and stuff like that. It’s a pretty common issue and there’s lots of things to check.

I double checked and for the resolution part I didn’t find anything useful (only this: Screen.resolutions shows only 640x800 but that is not the case I am having)
And for the display part I didn’t find anything at all.

Multiple-display video card driver software is the second-flakiest software on the entire planet.

It is surpassed in flakiness only by HP all-in-one inkjet printer driver software.

Good luck.

Well that is not what I am trying to achieve. I just want to give players the option to choose their output display.

Display class allows you to spawn multiple windows with content in them. It does not allow you to move your main game window to a different display. There’s a whole thread on it: Switch Monitor at runtime

As for Screen.resolutions being empty in a built game: I’ve only seen that happen when running the game through remote desktop. Are you doing anything of the sorts?

no I am running it on my local computer.

Ok thanks for that, I am gonna try it

Ok I have a guess to why the screen resolutions may be wrong. I am applying my user settings at the start of the game. These Usersettings consist of fullscreen mode and resolution, as well as other settings.
Am I supposed to change the “playerPrefs” mentioned in the forum thread you linked? And if yes, how would I do that?

No, you don’t need to touch player prefs. Doing that is unsupported.

What exactly does Screen.resolutions return, and how do you print it out?

Ok, so I solved my problem. Due to the refresh rate parameter, each resolution had multiple entries which threw an error.
So this is solved.

So how do I move the fullscreen window to another monitor? There was no clear solution in the thread you linked (at least for me)

If you’re on Unity 2020 or earlier, you’ll have to P/Invoke into Windows MoveWindow API. In Unity 2021.2 we added a new method to the Screen class “MoveMainWindowTo” which allows you to do it without calling Windows APIs directly.

Ok, than I put a hold on that and just wait until 2021 is smooth enough for me to upgrade my project to it.

Thank you so much for your help, I really appreciate it :slight_smile:

I’ve been trying to understand how to use MoveMainWindowTo(), but the docs and even ChatGPT don’t seem to answer the questions:

  • How / where do I get the DisplayInfo object for the first argument
  • A general “how to” example

I have been trying to use the MoveWindow API, and it DOES move it, but in 1 frame, it snaps back to the main monitor.

Thanks for any help

ah nevermind - never fails that just after I actually break down and ask, I figure it out.

For anyone else, here’s just a quick example:

List<DisplayInfo> _displayInfos = new();
Screen.GetDisplayLayout(_displayInfos);

then, when you want to switch, you could pass an int:

var displayInfo = _displayInfos[displayIndex];
            Screen.MoveMainWindowTo(displayInfo, Vector2Int.zero);

Looks like you figured it out just as I was responding :).

I was able to get MoveMainWindowTo() to work, however, it exhibits the same behavior - it switches to the correct monitor, but snaps back immediately. Would you happen to know why or what I can change to fix that?

Do you have any scripts that interact with window placement? Something outside of this API call is probably forcing the window back.

I’m embarrassed. Yes. Works great now :wink: Thank you!