Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by scarletshark · May 02, 2012 at 04:42 AM · c#visualstudiohideprocessembed

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image scarletshark · May 02, 2012 at 01:48 PM 0
Share

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.

avatar image
1

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 ?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image scarletshark · May 02, 2012 at 02:20 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Starting serproxy Process from Unity 0 Answers

Creating Objects and Hiding when out of Range 1 Answer

Renderer on object disabled after level reload 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges