- Home /
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.
Answer by asafsitner · May 02, 2012 at 07:11 AM
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
Thank you for your help! I was having so much trouble. When you've started a standalone game in batchmode, is it possible to unhide the window? The example code above was intended to hide, and unhide the window.
Answer by Bunny83 · May 02, 2012 at 02:15 PM
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 ?
I had not considered moving the window - although I'm not sure that would work. Seems I would have to still WaitForInputIdle to grab the Process's handle, so there would still be an ugly flash.
I'm sitting this process inside a winforms application, and the result looks perfect. It would simply be nice if I didn't have to see the window flash before it gets parented to the form.
To your last question - no. We own Unity Pro.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Starting serproxy Process from Unity 0 Answers