在WPF中嵌入Unity应用程序未显示

时间:2019-01-04 15:44:50

标签: c# wpf unity3d

我有一个Unity .exe,它与项目exe(bin / debug /)位于同一文件夹中。我按照here的说明进行操作,该exe启动了,但是没有嵌入WPF项目中。

过程代码在这里:

 internal delegate int WindowEnumProc(IntPtr hwnd, IntPtr lparam);

    [DllImport("user32.dll")]
    internal static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc func, IntPtr lParam);

    [DllImport("user32.dll")]
    static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

    private Process process;

    private IntPtr unityHWND = IntPtr.Zero;

    private const int WM_ACTIVATE = 0x0006;
    private readonly IntPtr WA_ACTIVE = new IntPtr(1);
    private readonly IntPtr WA_INACTIVE = new IntPtr(0);



    public UnityUserControl()
    {
        InitializeComponent();
        try
        {
            process = new Process();
            process.StartInfo.FileName = "REDACTED.exe"; //Done because of NDA
            process.StartInfo.Arguments =
                "-parentHWND " + UnityPanel.Handle.ToInt32() + " " + Environment.CommandLine;
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.CreateNoWindow = true;

            process.Start();

            ActivateUnityWindow(UnityPanel.Handle);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }
    private int WindowEnum(IntPtr hwnd, IntPtr lparam)
    {
        unityHWND = hwnd;
        ActivateUnityWindow(hwnd);
        return 0;
    }

它是这样托管的:

<WindowsFormsHost>
    <view:UnityUserControl x:Name="UnityControl"/>
</WindowsFormsHost>

任何帮助,将不胜感激!

0 个答案:

没有答案