Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to manage the application window's depth when launching an external process

Discussion in 'Scripting' started by jmdeb, Jul 7, 2022.

  1. jmdeb

    jmdeb

    Joined:
    Jul 28, 2017
    Posts:
    23
    Hello everyone,
    My application (A) launches from time to time another application (B).
    I want A, which is full screen, to go to a lower 'level' when B is open...so that I can see B!
    On Windows I can do this with the following code:
    Code (CSharp):
    1. IntPtr handle = new IntPtr(); // Handle to the window
    2.  
    3. [DllImportAttribute("user32.dll")]
    4. public static extern IntPtr GetActiveWindow();
    5. [DllImportAttribute("user32.dll")]
    6. public static extern IntPtr FindWindow(string className, string windowName);
    7. [DllImport("user32.dll")]
    8. public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);    
    9. [DllImportAttribute ("user32.dll")]
    10. public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    11. static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
    12. static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
    13. const UInt32 SWP_NOSIZE = 0x0001;
    14. const UInt32 SWP_NOMOVE = 0x0002;
    15. const UInt32 SWP_SHOWWINDOW = 0x0040;
    16. const UInt32 SWP_HIDEWINDOW = 0x0080;
    17.  
    18. // Get window handle
    19. handle = GetActiveWindow();
    20. if (handle == IntPtr.Zero)
    21. {
    22.     handle = FindWindow(null, "YourApplicationName");
    23. }
    24.  
    25. // I also need to be able to minimize/maximize the window of the 'main' application
    26. public void ActionMinimize()
    27. {
    28.     ShowWindowAsync(handle, 2); //SW_SHOWMINIMIZED(2)
    29. }
    30. public void ActionMaximize()
    31. {
    32.     ShowWindowAsync(handle, 3); //SW_SHOWMAXIMIZED(3)
    33. }
    34.  
    35. // Launch external application and manage this app depth
    36. public void ActionLaunch()
    37. {
    38.     SetWindowPos(handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
    39.  
    40.     // ...launch external application inbetween
    41.  
    42.     SetWindowPos(handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
    43. }

    Now that I'm porting my application to MacOs the question arises to do the same thing for this platform. Would you be so kind as to give me some hints on how I can achieve the same result?
    Thank you!
     
    Last edited: Jul 7, 2022
  2. bgulanowski

    bgulanowski

    Joined:
    Feb 9, 2018
    Posts:
    35
    You might start here:

    https://developer.apple.com/documentation/appkit/nswindow/level

    In AppKit, NSWindows have a level property, which determines what layer of the windowing environment they live in. You can maybe use WindowLevel.submenu or .floating. I haven't used this in a long time, and have no idea what the actual ordering is.

    Once upon a time, if a fullscreen app took over the whole screen, this would not work. But if your full screen app is doing it the normal way, now (hiding the window and menu chrome), I think it will work.
     
  3. jmdeb

    jmdeb

    Joined:
    Jul 28, 2017
    Posts:
    23
    I managed to understand better what was happening: in fact when Unity, launched in full screen, launches another application also in full screen, the Unity window is minimized in the dock!
    So I have to find a way to restore the Unity window in the dock once the secondary application is finished.
    After searching the whole web I managed to find the ONLY thing that almost does it!
    The window is maximized...but not in full screen.
    What works so simply, by clicking on the icon in the dock, is apparently very complicated to replicate in code!
    Here is what works 'almost':
    Code (CSharp):
    1. // Ref.: http://string.quest/read/13900624
    2. BOOL old_isReleasedWhenClosed = [win isReleasedWhenClosed];
    3. [win setReleasedWhenClosed:false];
    4. [win close];
    5. [win makeKeyAndOrderFront: nil];
    6. [win setReleasedWhenClosed:old_isReleasedWhenClosed];
    Please, could you help me to (so) make a Unity window that is in the Dock...full screen?
     
  4. bgulanowski

    bgulanowski

    Joined:
    Feb 9, 2018
    Posts:
    35
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,851
    But having found something in Apple's reference documentation, how do you convert that to Unity code? I've found lots of examples of doing this on Windows using [DllImport] or [DllImportAttribute], but no examples of doing this on Mac (or Linux). I'm sure I must be overlooking some examples or tutorials somewhere... can you point me in the right direction?

    (If it matters or helps, my need is simply to set the window title: https://developer.apple.com/documentation/appkit/nswindow/1419404-title)