internal static IntPtr FindWindowWithThreadProcessId(int processId)
        {
            var window = new IntPtr();

            Win32Helpers.EnumWindows(delegate(IntPtr wnd, IntPtr param)
            {
                var windowProcessId = 0;
                Win32Helpers.GetWindowThreadProcessId(wnd,
                                                      out windowProcessId);
                if (windowProcessId != processId || !IsMainWindow(wnd))
                {
                    return(true);
                }

                window = wnd;
                return(false);
            },
                                     IntPtr.Zero);

            if (window.Equals(IntPtr.Zero))
            {
                Debug.LogError("Could not find any window with process id " +
                               processId);
            }

            return(window);
        }
        internal static IntPtr GetGameViewWindowHandle(int processId)
        {
            const string GameViewCaption         = "UnityEditor.GameView";
            const string UnityContainerClassName = "UnityContainerWndClass";

            var window = new IntPtr();

            Win32Helpers.EnumWindows(delegate(IntPtr hWnd, IntPtr lParam)
            {
                if (!Win32Helpers.IsWindowVisible(hWnd))
                {
                    return(true);
                }

                var windowProcessId = 0;
                Win32Helpers.GetWindowThreadProcessId(hWnd,
                                                      out windowProcessId);

                if (windowProcessId == processId)
                {
                    var builder = new StringBuilder(256);
                    Win32Helpers.GetClassName(hWnd, builder, 256);

                    if (builder.ToString() == UnityContainerClassName)
                    {
                        Win32Helpers.EnumChildWindows(hWnd,
                                                      delegate(IntPtr childHwnd, IntPtr childParam)
                        {
                            if (!Win32Helpers.IsWindowVisible(childHwnd))
                            {
                                return(true);
                            }

                            var childLength =
                                Win32Helpers.GetWindowTextLength(childHwnd);
                            if (childLength == 0)
                            {
                                return(true);
                            }

                            var childBuilder =
                                new StringBuilder(childLength);
                            Win32Helpers.GetWindowText(childHwnd,
                                                       childBuilder, childLength + 1);

                            if (childBuilder.ToString() == GameViewCaption)
                            {
                                //Found it!
                                window = childHwnd;
                                return(false);
                            }

                            return(true);
                        },
                                                      IntPtr.Zero);
                    }
                }

                return(true);
            }, IntPtr.Zero);

            if (window.Equals(IntPtr.Zero))
            {
                Debug.LogError("Could not find game view!");
            }

            return(window);
        }