Drawing Debug Text at a Component's Location in the Viewport

I’m trying to draw debug text on a component location, but the position of the text is distorted.

Strangely, it worked fine once without any code modifications, but when I rerun the engine, the problem returned.



void FSpawnerComponentVisualizer::DrawVisualizationHUD(
	const UActorComponent* Component,
	const FViewport* Viewport,
	const FSceneView* View,
	FCanvas* Canvas)
{
	const USpawnerComponent* SpawnerComp = Cast<USpawnerComponent>(Component);

	// 거리가 설정 값 보다 가까울때만 통과 
	const FVector SpawnerLocation = SpawnerComp->GetComponentLocation();
	const float Distance = FVector::Dist(View->ViewLocation, SpawnerLocation);
	if (Distance > UFongcrushSettings::GetMaxDebugDrawDistance()) return;

	// SpawnerComp가 시야 내부에 위치할 경우에만 통과
	if (!View->ViewFrustum.IntersectSphere(SpawnerLocation, 1.f)) return;
	
	FVector2D ScreenPos;
	if (View->WorldToPixel(SpawnerLocation, ScreenPos))
	{		
		const UClass* SpawnerClass = SpawnerComp->GetSpawnActorClass();
		
		FString DebugString;
		if (SpawnerClass)
		{
			DebugString += FString::Printf(TEXT("(%s)\n"), *ScreenPos.ToString());
			DebugString += FString::Printf(TEXT("SpawnClass: %s\n"), *SpawnerComp->GetSpawnActorClass()->GetName());
			DebugString += FString::Printf(TEXT("SpawnTime: %.2f"), SpawnerComp->GetSpawnTime());
		}
		
		FCanvasTextItem TextItem(
			ScreenPos,
			DebugString.IsEmpty() ? FText::FromString("None") : FText::FromString(DebugString),
			GEngine->GetSmallFont(),
			FLinearColor::White);
		
		TextItem.bCentreX = true;
		TextItem.bCentreY = true;
		
		Canvas->DrawItem(TextItem);
	}
}

Solved!

ScreenPos /= Canvas->GetDPIScale();