Unity: Start the game on a specific monitor indicated externally

320 views Asked by At

I'm developing a game in Unity that has an external launcher. For multi-monitor compatibility, I want to have the game start on the same screen as the launcher's window (the midpoint of the window if spanning multiple monitors).

I looked into the -monitor command line argument, but the numbers didn't correspond at all to the order in which the launcher enumerated the monitors. (The launcher is an Electron app using window.getScreenDetails() to get the display layout.)

My next idea was to pass just the point into Unity and handle iterating the displays and going to the display that contained the specific point, only to find out that, as far as I can tell, Unity doesn't have any concept of multiple displays forming a cohesive desktop. All coordinates in the Unity screen API are relative to a specific display, neither does the DisplayInfo class contain any information about the position of the display relative to the others.

That being said, how can I communicate to my Unity game where specifically I want it displayed?

1

There are 1 answers

8
CBX_MG On

In Unity you can set the monitor index using: PlayerPrefs.SetInt("UnitySelectMonitor", index);

It'll probably be harder to communicate the index to the Unity application itself though, there fore I'd set the index in the registry directly before starting the application. Since PlayerPrefs are located in the system registry itself that should work quite nicely. You can find OS depending locations in the API: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

I don't know for sure how to get the monitor index of this specific launcher, but quickly looking through the API my guess would be using screen.getPrimaryDisplay(). The Id of the return value is probably the monitor index. https://www.electronjs.org/docs/latest/api/screen#screengetprimarydisplay
Otherwise have a look in the registry as well if there's a value stored for the launchers monitor index.