通過AHud顯示幀數率

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;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章