Is it possible to label components in the Editor?

Currently i am working with an visualizer module ([link][1]) to visualize my components. But i want to add additional information as Text in the World like it is possible in Unity (following picture) without adding components to the actor and only visible in editor view or debug view.

I think it is an essential thing during development to use, so there must be a way to easily archive this but i dont find any solutions for this in the web or engine. Can somebody please help me and give me some hints or a solution?

Please only C++ solutions.

[Edit]

Okay I found that the FComponentVisualizer class has a DrawVisualizationHUD(const UActorComponent* Component, const FViewport* Viewport, const FSceneView* View, FCanvas* Canvas) function. But I don’t get how to use it. Does anyone please have an idea?

Hi. Component visualizer works a bit different. It diplays only selected components as I know. If you want to draw something persistant to display your components you have to use a component render proxy (it responsible both for debug and realtime drawing. ) To find out how to do this you can see a Spline component code already existing in the engine. Actually you have to override CreateSceneProxy() function to create and return your own scene proxy that will draw anything you need.

I have the same problem.

At first sight DrawVisualizationHUD() seems like the correct way of doing it.

But it does not work at all. No matter what you draw on the supplied Canvas, it never shows on screen. Flushing the canvas does not help.

Searching the internet returns zero examples of using DrawVisualizationHUD() to do anything at all. Searching Unreal Engine repository also does not help - the DrawVisualizationHUD() method is there, but never used in any visualisers.

This answer is not useful at all.

The question was about text labels in a component visualizer. It is a perfectly valid intention - sometimes you need to visualize something only in Blueprint editor viewport, but not in game, and you may need some text labels. Component visualizer is the correct place to do it.

Component scene proxy is used for rendering in game. But having debug rendering code in gameplay component may be undesirable for performance reasons or for code clarity reasons.

Besides, it is not clear how to render text labels via scene proxy (I wonder if it is possible at all?) For text labels, you usually need an FCanvas (or a HUD, that internally uses FCanvas). Functions like DrawDebugString() use a debug HUD connected to PlayerController, but it is available only in game, not in BP editor viewport.

I should mention that DrawVisualizationHUD() does not work as of 4.19.
Did not try 4.20 yet.

I’m probably a bit late to help the OP, but hopefully this is helpful for others.

To draw text in the HUD from a component visualizer, look at EdMode.cpp’s DrawHUD method, under the if (UsesPropertyWidgets()), which gives an example of drawing text to the viewport.

Or, just use this code (which is almost identical to the code in EdMode.cpp, and also used in various other places in the engine):

void (YourClass)::DrawVisualizationHUD(const UActorComponent* Component, const FViewport* Viewport, const FSceneView* View, FCanvas* Canvas)
{
    FVector PositiveLocation = FVector(200, 200, 200);
    const int32 HalfX = 0.5f * Viewport->GetSizeXY().X;
    const int32 HalfY = 0.5f * Viewport->GetSizeXY().Y;

    const FPlane Proj = View->Project(PositiveLocation);
    if (Proj.W > 0.f)
    {
        const FString DisplayString = TEXT("P");
        const int32 XPos            = HalfX + (HalfX * Proj.X);
        const int32 YPos            = HalfY + (HalfY * (Proj.Y * -1.f));
        FCanvasTextItem TextItem(FVector2D(XPos + 5, YPos), FText::FromString(DisplayString), GEngine->GetSmallFont(), FLinearColor::White);
        TextItem.EnableShadow(FLinearColor::Black);
        Canvas->DrawItem(TextItem);
    }
}
1 Like