Unreal Engine编辑器模式下如何向视口打印信息?
Unreal Engine编辑器模式下视口打印信息的解决方法
因为AddOnScreenDebugMessage属于Gameplay层API,仅在PIE(Play In Editor)模式生效,编辑器模式下需要使用Editor层的相关API来实现视口打印,以下是几种可行方案:
方案一:使用DrawDebugString配合编辑器视口上下文
通过获取当前激活的编辑器视口,调用引擎的调试绘制函数在视口指定位置输出文本:
// 获取当前激活的编辑器视口客户端 FEditorViewportClient* ViewportClient = GEditor->GetActiveViewportClient(); if (ViewportClient) { UWorld* World = ViewportClient->GetWorld(); if (World) { // 参数依次为:World、文本位置、文本内容、附着Actor(可为nullptr)、文本颜色、显示时长 DrawDebugString(World, FVector(0, 0, 0), TEXT("编辑器调试信息"), nullptr, FColor::Green, 3.0f); } }
方案二:自定义编辑器模式的Render函数
如果你正在开发自定义编辑器模式,可以重写模式的Render函数,直接在视口绘制文本:
void FYourCustomEditorMode::Render(const FSceneView* View, FViewport* Viewport, FPrimitiveDrawInterface* PDI) { // 先调用父类的Render逻辑 FEditorModeTools::Render(View, Viewport, PDI); // 定义文本位置、字体、颜色等参数 const FVector2D TextPosition = FVector2D(20, 20); const FSlateFontInfo DebugFont(FPaths::EngineContentDir() / TEXT("Slate/Fonts/Roboto-Regular.ttf"), 18); const FLinearColor TextColor = FLinearColor::White; // 在视口绘制文本 FSlateDrawElement::MakeText( PDI->GetDrawElements(), Viewport->GetRenderLayer(), DebugFont, TEXT("自定义编辑器模式调试信息"), TextPosition, TextColor ); }
方案三:使用编辑器控制台消息API
通过FEditorConsoleUtils直接在编辑器视口顶部显示类似控制台的消息:
// 参数依次为:消息内容、文本颜色、显示时长(秒) FEditorConsoleUtils::DisplayConsoleMessage(TEXT("编辑器模式提示"), FColor::Cyan, 4.0f);
内容的提问来源于stack exchange,提问作者赵一霖
相关产品推荐
相关产品推荐

