Excessive CPU usage when hiding window of standalone player

Hi all,

as the title already suggests I am experiencing an issue, where the unity process suddenly uses an excessive amount of CPU when I hide the game’s window.

This is my setup:
I develop a WPF application (the parent), with the unity game being just a child process of the main application. Therefor, when I start the game, I pass the CommandLine-Argument to the game process to embed the standalone playern in my main application (see Unity - Manual: Command-line arguments):

_gameProcess.StartInfo.CreateNoWindow = true;
_gameProcess.StartInfo.Arguments += "-parentHWND " + mainWindowHandle.ToInt32() + " " + Environment.CommandLine + " ";

So far everything works fine. But I also needed some pause/resume functionality without always quitting the game entirely. So my mechanism for halting the game is as follows:

  1. Parent sends a message to the game so that it pauses itself
  2. game will Load a blank scene and set the Time.timeScale to 0f.
  3. Parent hides the game window using calls to the User32.dll:
// in the wrapper for the User32.dll
internal const int SW_HIDE = 0;
internal const int SW_Show = 5;

[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hwnd, int show);

// hiding the game
SendMessgeToGame("stop"); // Communication is done via Anonymous Pipes
User32.ShowWindow(_gameWindowHandle, User32.SW_HIDE);

Now comes the strange part:
At first, my game process used approximately 1.3% of CPU. After hiding the window it suddenly uses round about 30%. I already tried not hiding the game and just switching to the blank scene which will reduce the CPU usage to about 0.7%. Also, different ShowWindow-Flags like SW_Minimize and the like did not help either.

Did anyone of you experience this issue before? I am happy for every hint/opinion :slight_smile:

P.S.: If someone needs more detailed information let me know!

Edit:
I just tried the same mechanism with notepad++ being the child process. CPU usage stays unchanged, so my guess would be that this is an issue within Unity.

Fixed this by doing Thead.Sleep in Unity to force 60 FPS
Application.targetFrameRate = 60 did not work
QualitySettings.vSyncCount = 1 did not work