- Home /
Choose screen with Command line arguments
I am planning to run two or more instances of a "game" at a single PC and I need to start this automatically using command line scripts.
I start in windowed border-less mode using a FullHD resolution. According to http://docs.unity3d.com/Manual/CommandLineArguments.html this command should work, but it shows up at my first monitor no matter what. Maybe this just works in fullscreen?
game.exe -popupwindow -screen-width 1920 -screen-height 1080 -adapter 2
Is there any other way to force the "game" to show on other monitors in windowed mode?
Answer by PhilTuffler · Apr 09, 2015 at 04:13 AM
The -adapter switch does not work in windowed mode. I have made a workaround using user32.dll to position the window at a deifined position
game.exe -popupwindow -screen-width 1920 -screen-height 1080 -screen-position-x 1920 -screen-position-y 0
the C# code needed is:
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
private static string[] startupArgs;
public class PlaceWindow : MonoBehaviour
{
#if UNITY_STANDALONE_WIN || UNITY_EDITOR
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern bool SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
public static void SetPosition(int x, int y, int resX = 0, int resY = 0)
{
IntPtr hWnd = GetForegroundWindow();
SetWindowPos(hWnd, 0, x, y, resX, resY, resX * resY == 0 ? 1 : 0);
}
#endif
public static string getStartupParameter(String parameter)
{
if(startupArgs == null)
startupArgs = Environment.GetCommandLineArgs();
string s = null;
for(int i = 0; i < startupArgs.Length; i++)
{
if(startupArgs[i].Equals(parameter) && startupArgs.Length >= i+2)
s = startupArgs[i+1];
}
return s;
}
// Use this for initialization
void Awake ()
{
int x = int.Parse(getStartupParameter("-screen-position-x"));
int y = int.Parse(getStartupParameter("-screen-position-y"));
SetPosition(x,y);
}
}
Answer by pachermann · Feb 27, 2020 at 01:08 PM
This is amazing, thank you so much for this solution!
Your answer
Follow this Question
Related Questions
Simultaneous Unity & Command Prompt? 0 Answers
[Help] FullScreenMode - what is it? 0 Answers
Class could not be found when executing method from command line (-executeMethod) 0 Answers
sign apk from jenkins/commandline,how to sign android apk through jenkins/command line 0 Answers
Lock mouse to application. 1 Answer