简体   繁体   中英

Desynchronize refresh of a Unity window embedded in a WPF application with HwndHost

I'm working on a WPF application embedding a Win32 application (Unity window) thanks to a HnwdHost.

For performances reasons, we are obliged to set the framerate of the unity window to 30 FPS. However, when the other user controls of the WPF application try to display animations, these animations are not fluent (eg. when I try to quickly highlight several buttons, the highlighting takes time to follow the sursor of my mouse). And if I set the framerate of the Unity window to 500, I have not any problem anymore.

This is how I analyse this situation : During the animation, the WPF GUI seems to try to refresh all its controls. However, as the framerate of the Unity host (HnwdHost) is to slow, it takes a great time to be refreshed, and all the other controls seems to wait for it.

So, do you know if there is a way to desynchronize the render of the WPF application and the render of the Unity window (embbeded thanks to HwndHost) ?

The parentHWND cmdline argument lets the unity player attach its window with SetParent , which attaches the message queues of the two UI threads. They then send blocking messages, which leads to one process blocking the rendering of the other.

We solved it by detaching the message queues using AttachThreadInput .

// detach thread message queues so slow unity rendering won't block wpf rendering
var currentThreadId = GetWindowThreadProcessId(hwndParent.Handle, IntPtr.Zero);
var unityThreadId = GetWindowThreadProcessId(unityHWND, IntPtr.Zero);
AttachThreadInput(currentThreadId, unityThreadId, false);
AttachThreadInput(unityThreadId, currentThreadId, false);

Of course this means required window messages must be posted manually afterwards. Window resizing still works when using the WPF HwndHost, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM