Set Startup Monitor

What it the current preferred way to set which monitor(display), in a multi monitor setup, the game starts on?

I have a dropdown on my setting screen populated with info from the Display.displays array.

When a user selects “Display 2” for example, I want the game to use that Display at startup. Switching at runtime would be great, but right now I’d be happy to get it using the selected display next time the app starts.

I found various threads on this topic but many seem dated and not super useful. Thought I’d post here to try and get the latest thoughts on the topic. Is anyone doing this successfully in 2019.3.x?

-Jeff

Which platform is this for?

Windows and Mac (Steam)

So there is “-monitor” you can pass to Unity on startup either via command line parameter if you have a game launcher OR you can recompile the executable and add logic there to pass correct value to UnityMain.

At runtime, you can use MoveWindow function on Windows. I’ll ask around if there’s something similar on Mac.

1 Like

Looks like on Mac the easier way to do this is to write a plugin Objective C and call “[[NSApplication sharedApplication] mainWindow]” to retrieve the main window. From there, you can call setFrame.

Are there any plans to make this easier? Seems like pretty common scenario. I’ve never written an Objective C plugin and not sure how to call MoveWindow from Unity/C#

Has anyone else handled this or does everyone just let the app open however it opens by default?

Thanks for the response, though.

-Jeff

Is this suppose to work?

PlayerPrefs.SetInt(“UnitySelectMonitor”, index); // Select monitor at index

I’ve been messing with it but it seems a bit un-reliable.

No, changing that player setting won’t work.

You can define and call MoveWindow like this:

[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();

[DllImport("user32.dll", SetLastError = true)]
static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

void CallMoveWindow(ref RectInt coords)
{
    MoveWindow(GetActiveWindow(), coords.x, coords.y, coords.width, coords.height, true);
}

Unfortunately, we don’t have any plans to make this easier short term… we want to solve the multiple displays problem at some point but it’s not happening for at least a couple Unity releases.

1 Like