Open Unity Game as a Hidden Process in Visual C#

Disclaimer: This is intended for use with Unity Pro. We have no intentions of hiding the Unity splash screen, or circumventing any free version features. Please be aware that we intend to comply entirely with all Unity3D terms.

I have a compiled PC Unity game, and I’m trying to start it as a hidden process in Visual C#. The following is the code I am using:

//Initialization
private const int SW_SHOW = 5;
private ProcessStartInfo info = new ProcessStartInfo();
private Process p;
private string exeName = "myGame.exe"; 
private IntPtr HWND;

//to find the hidden window
[DllImport("user32.dll",SetLastError=true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

//to show the hidden window
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hwnd, WindowShowStyle nCmdShow);

//to make the hidden window interactive 
[DllImport("user32.dll")]
private static extern bool EnableWindow(IntPtr hwnd, bool enabled);

And in my main function:

info.FileName = this.exeName;
info.UseShellExecute = true;
info.WindowStyle = ProcessWindowStyle.Hidden;

p = Process.Start(info);
p.WaitForInputIdle();
HWND = FindWindow(null, "myGame");

System.Threading.Thread.Sleep(1000);    //to test the hidden window
        
ShowWindow(HWND, SW_SHOW);
EnableWindow(HWND, true);

When I run this code with an arbitrary windows program like “notepad” instead of “myGame.exe”, I get the desired result. However, when I run a Unity Game in this way, the window flickers before being hidden.

Why is this happening? It seems fairly clear that Unity listens to nCmdShow. Otherwise, the window would not hide at all.

Apologies… I know this is more of a Visual Studio question than a Unity question.

Unity has support for command line arguments, and one of them, namely -batchmode starts the Unity application as ‘hidden’, with no renderer enabled and few other things.

To make it happen from VC# you can add the arguments when starting the program.

Take a look at the available arguments here

Unity has to create a device context. This could by the issue. The only alternative might be to move the window outside of the screen and back in after some time. I just ask myself why ou would need something like that?

Next-gen-trojan-horse with 3D support?!?

Or do you just want to “hide” that you used the free version ?