AHud中有个DrawHUD,在这个函数中做的绘制每帧都会清除掉再重绘,可以做一些帧率显示或者辅助调试的绘制工作,不必做专门的清除管理。拿帧率显示为例,在这里我们做25帧刷新一次的控制,也可以每帧刷新,看自己需要。
MyHUD.h
UCLASS()
class AMyHUD : public AHUD
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HUDFont) UFont* hudFont;
virtual void DrawHUD() override;
void Tick(float DeltaTime);
protected:
void BeginPlay();
protected:
int32 FrameCount;
float FrameTime;
float FrameRate;
};
void AMyHUD::BeginPlay()
{
Super::BeginPlay();
FrameCount = 0;
FrameRate = 60;
}
MyHUD.cpp
void AMyHUD::DrawHUD()
{
Super::DrawHUD();
APlayerController* PC = GetWorld->GetFirstPlayerController();
FString FPS = "FPS:" + FString::FromInt((int32)FrameRate);
int32 sizeX, sizeY;
PC->GetViewportSize(sizeX, sizeY);
float textSizeX = sizeX / 1920.0 * 1.0;
float textSizeY = sizeY / 1080.0 * 1.0;
float textSize = (textSizeX < textSizeY) ? textSizeX : textSizeY;
DrawText(FPS, FColor::White, sizeX * 0.03, sizeY * 0.87, hudFont, textSize);
}
void ATRVideoFusionHUD::Tick(float DeltaTime)
{
if (FrameCount > 25)
{
FrameRate = 24 / FrameTime;
FrameCount = 0;
FrameTime = 0.0;
}
FrameCount++;
FrameTime += DeltaTime;
}