使用UE4创建C++工程,常常使用屏幕Debug功能,函数形式:
/**
* From "UnrealEngine.cpp"
* This function will add a debug message to the onscreen message list.
* It will be displayed for FrameCount frames.
*
* @param Key A unique key to prevent the same message from being added multiple times.
* @param TimeToDisplay How long to display the message, in seconds.
* @param DisplayColor The color to display the text in.
* @param DebugMessage The message to display.
*/
void UEngine::AddOnScreenDebugMessage(uint64 Key, float TimeToDisplay, FColor DisplayColor, const FString& DebugMessage, bool bNewerOnTop, const FVector2D& TextScale)
该函数中TimeToDisplay、DisplayColor、DebugMessage、TextScale四项参数较好理解,分别表示持续时间、字体颜色、信息内容、字体缩放。使用时按照需求将参数填入相应位置即可。下面具体说明Key、bNewerOnTop两个参数的用法及功能:
- uint64 Key
用来防止相同信息被显示多次:当Key为0时,相同的信息只在界面上显示一次;当Key为-1时,相同信息会按照程序执行依次显示。如下:
//Key为0,相同信息只显示1次
for (int i = 0; i < 5; ++i)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(0, 5.0f, FColor::Yellow, TEXT("Hello world"));
}
}
//Key为-1,相同信息显示5次
for (int i = 0; i < 5; ++i)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Hello world"));
}
}
- bNewerOnTop
用来确定一系列信息显示的位置:当bNewerOnTop=true时,最新的信息永远显示在顶部;当bNewerOnTop=false时,最新的信息显示在底部,缺省时默认为true。如下:
//bNewerOnTop=true 最新的信息永远显示在顶部
for (int i = 0; i < 5; ++i)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Yellow, FString::Printf(TEXT("Hello world, this is %d"), i), true);
}
}
//bNewerOnTop=false 最新的信息永远显示在底部
for (int i = 0; i < 5; ++i)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Yellow, FString::Printf(TEXT("Hello world, this is %d"), i), false);
}
}