Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Costiome · Nov 04, 2016 at 11:05 AM · process

Switch between two Unity applications

Hi,

I make two Unity applications who called each others, let's name them "app A" and "app B".

I coded a script using C# library, as seen as this page. It will switch between apps when clicking on an object.

Here is the code :

 public class launchExternalApp : MonoBehaviour {
 
     /*
      * C# imports from user32.dll
      */
     [System.Runtime.InteropServices.DllImport("user32.dll")]
     public static extern int SetForegroundWindow(IntPtr hwnd);
 
     [System.Runtime.InteropServices.DllImport("user32.dll")]
     public static extern int SetActiveWindow(IntPtr hwnd);
 
     [System.Runtime.InteropServices.DllImport("user32.dll")]
     static extern bool ShowWindow(IntPtr hWnd, uint flag);
 
     [System.Runtime.InteropServices.DllImport("user32.dll")]
     public static extern IntPtr FindWindow(string className, string windowTitle);
 
     [System.Runtime.InteropServices.DllImport("user32.dll")]
     static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
     /**/
 
     private enum ShowWindowEnum
     {
         Hide = 0,
         ShowNormal = 1, ShowMinimized = 2, ShowMaximized = 3,
         Maximize = 3, ShowNormalNoActivate = 4, Show = 5,
         Minimize = 6, ShowMinNoActivate = 7, ShowNoActivate = 8,
         Restore = 9, ShowDefault = 10, ForceMinimized = 11
     };
 
     public string windowName = "app_A";
     public string processPathExe ="D:/Unity/Utils/Build/app_a/app_A.exe";
 
     void OnMouseUpAsButton()
     {
         IntPtr wdwIntPtr = (IntPtr) (-1) ;
         wdwIntPtr = FindWindow(null, windowName);
         
         if (wdwIntPtr == (IntPtr)0)
         {
             Process.Start(processPathExe);
         }
         else
         {
             ShowWindow(wdwIntPtr, (uint)ShowWindowEnum.Show);  // Make the window visible if it was hidden
             ShowWindow(wdwIntPtr, (uint)ShowWindowEnum.Restore);  // Next, restore it if it was minimized
             SetForegroundWindow(wdwIntPtr);  // Finally, activate the window 
         }
         
     }
 }

This script works differently according to application window state :

  • If both applications are windowed, this works fine.

  • if one application is windowed and the other one is fullscreen, it works fine.

  • if both are in fullscreen, clicking on the object will flash the other app, then back to my desktop.

There's a conflict bewteen two fullscreen applications, how can i manage that ?

Thanks,

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Skuuulzy · Apr 30, 2021 at 02:09 PM

Hello, i now it's a bit late but i think i find a way to avoid the "flash" when switching focus. You just have to slightly delay your ShowWindow method.

Indeed i experienced the same issues and after using this delay no more flash.

 private void FocusProcess(Process process)
 {
     var launchTime = DateTime.Now.TimeOfDay - process.StartTime.TimeOfDay;
     Debug.Log("Process launch time" + launchTime);
     if (launchTime.Seconds > processFocusDelay)
     {
         // Immediate Focus
         Debug.Log("Immediate Focus");
         StartCoroutine(DelayProcess(process, 1));
     }
     else
     {
         // Focus after the process focus delay is over
         int remainingTime = Mathf.RoundToInt(processFocusDelay - launchTime.Seconds);
         Debug.Log("Delayed Focus : " + remainingTime);
         StartCoroutine(DelayProcess(process, remainingTime));
     }
 }

 private IEnumerator DelayProcess(Process process, int delay)
 {
     yield return new WaitForSeconds(delay);
     Debug.Log("Focus");
     IntPtr hWnd = FindWindow(null, process.ProcessName);
     ShowWindow(hWnd, 3);
 }

As you can see in the example above even if i want to "immediately" focus my new process i wait for 1 seconds before focus. I think it's possible to wait lesser than 1 sec but it work for me !

With Love, Skuuulzy

Comment
Add comment · 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

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

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to use shared memory in Unity ? 3 Answers

Can’t open project from usb as it says opening file failed 0 Answers

Using -parentHWND makes everything slow ..? 0 Answers

Unity debugger hangs when using System.Diagnostics.Process 0 Answers

Can I force BaseInputModule.Process to fire when window does not have focus? 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