Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Unity 5 Windows 7 application focus/fullscreen problem [Workaround]

Discussion in 'Editor & General Support' started by greg-harding, May 21, 2015.

  1. greg-harding


    Apr 11, 2013
    hey everyone,

    our fullscreen Unity 5.0.2f1 Windows 7 (x64) standalone application starts up and runs fine when double-clicking on the app icon - the resolution dialog is hidden by default so the app runs fullscreen and everything runs as expected.

    Unfortunately, we have a problem when starting the app via Task Scheduler - it's an installation so the app is set to start 1 minute after booting up and is the last thing run. The app starts ok but doesn't get the correct focus. Instead, it starts with the windows taskbar and some other commandline windows showing over the top of it. The player is set to run in the background and fullscreen etc. so the app itself starts working, but obviously we want to get it focused correctly so it's running in the foreground on top of everything else.

    Is this a known problem when a standalone player is started via Task Scheduler or some other batchfile? Are there any commandline arguments we can use to workaround the problem?

    We've tried using some window focus/show code but it's not working either. Here's a modified snippet that tries to get the main window handle for the current process and then switch to it. The problem is that the Unity process is not returning a window handle - the hWnd returned is 0 - so nothing can be switched to. I assumed that we should be able to get a valid hWnd for the Unity player main window handle but the variations of this and other snippets we've tried so far always return 0.

    Thanks for any info or help.

    // greg

    Update: hWnd fix. An alternative method for getting the hWnd does return a useful value. I've updated the snippet below to help anyone else that stumbles into this.

    Update 2: A workaround. Disabling the registry setting that prevents windows from stealing focus and using the updated code snippet below to set the app window to the foreground seems to work. Information on the registry setting is at Basically, use regedit to set HKEY_CURRENT_USER > Control Panel > Desktop > ForegroundLockTimeout to 0. This allows the call to SetForegroundWindow(hWnd) to succeed instead of flashing task bar icons and keeping the app in the background.

    Code (CSharp):
    2. [DllImport("user32.dll")]
    3. public static extern IntPtr FindWindow(string className, string windowName);
    5. [DllImport("user32.dll")]
    6. internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);
    8. [DllImport("user32.dll")]
    9. internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    11. private void GetFocusWin() {
    12.     // get hWnd, nb. hWnd always seems to return 0 with this method
    13.     //
    14.     //System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
    15.     //IntPtr hWnd = currentProcess.MainWindowHandle;
    17.     // get hWnd, nb. this works ok on Windows 7 x64
    18.     //
    19.     IntPtr hWnd = FindWindow(null, "Your App Name");
    21.     if (hWnd != IntPtr.Zero) {
    22.         Debug.Log("GetFocusWin() setting foreground window, showing window SW_SHOWDEFAULT");
    24.         SetForegroundWindow(hWnd);
    25.         // enum values from
    26.         ShowWindow(hWnd, 10); // SW_SHOWDEFAULT = 10, SW_MAXIMIZE = 3, SW_SHOW = 5
    28.     } else {
    29.         Debug.LogWarning("GetFocusWin() failed, hWnd is 0!");
    30.     }
    31. }
    Last edited: May 22, 2015
    Justin-Wasilenko likes this.
  2. TimG.


    Jul 29, 2013
    Sir, you made my day. Thank you. I can confirm that this fix also works on Win 8.1
  3. pjbue


    Oct 8, 2016
    Works. Thank you!
  4. matheus_inmotionvr


    Oct 3, 2018
    Thank you so much for this solution. I can confirm this still works on Unity 2018.2.10 and Windows 10.
  5. Justin-Wasilenko


    Mar 10, 2015
    Thanks! Glad I found this page. Needed to get a reference to the Unity Window Handle for TrackIR to work with IL2CPP. Perfect!