编辑器模式下焦点不在窗口内的卡顿

在 Windows 等平台上运行 UE 时在编辑器内 Play 游戏,若当前的焦点不在编辑器 /VR 应用内,会减少对 CPU 的占用,从而出现卡顿的问题 (打包出来不会出现这个问题)。
其实是因为 UE 在编辑器环境下做了不在焦点内的检测:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Runtime/ApplicationCore/Private/Windows/WindowsPlatformApplicationMisc.cpp
void FWindowsPlatformApplicationMisc::PumpMessages(bool bFromMainLoop)
{
if (!bFromMainLoop)
{
TGuardValue<bool> PumpMessageGuard(GPumpingMessagesOutsideOfMainLoop, true );
// Process pending windows messages, which is necessary to the rendering thread in some rare cases where D3D
// sends window messages (from IDXGISwapChain::Present) to the main thread owned viewport window.
WinPumpSentMessages();
return;
}

GPumpingMessagesOutsideOfMainLoop = false;
WinPumpMessages();

// Determine if application has focus
bool HasFocus = FApp::UseVRFocus() ? FApp::HasVRFocus() : FWindowsPlatformApplicationMisc::IsThisApplicationForeground();

// If editor thread doesn't have the focus, don't suck up too much CPU time.
if(GIsEditor)
{
static bool HadFocus=1;
if(HadFocus && !HasFocus)
{
// Drop our priority to speed up whatever is in the foreground.
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
}
else if(HasFocus && !HadFocus)
{
// Boost our priority back to normal.
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
}
if(!HasFocus)
{
// Sleep for a bit to not eat up all CPU time.
FPlatformProcess::Sleep(0.005f);
}
HadFocus = HasFocus;
}
// ...
}

只需要把 FPlatformProcess::Sleep(0.005f); 这一行注释掉就可以了。

这个需求是因为想要在非 DS 架构下多开在编辑器进行测试,因为不在焦点就会 sleep,所以会造成收发包的卡顿。