可视化组件

发布: (2026年3月7日 GMT+8 14:23)
5 分钟阅读
原文: Dev.to

Source: Dev.to

创建编辑器模块

要创建编辑器模块,添加一个新文件夹,其名称应与运行时模块相匹配并带有后缀 EdEditor(例如,MyRuntimeMyRuntimeEditor)。
该文件夹应包含以下文件:

\Private\CustomModuleEd.cpp
\Public\CustomModuleEd.h
CustomModuleEd.Build.cs

CustomModuleEd.Build.cs

using UnrealBuildTool;

public class CustomModuleEd : ModuleRules
{
    public CustomModuleEd(ReadOnlyTargetRules Target) : base(Target)
    {
        PrivateDependencyModuleNames.AddRange(new string[]
        {
            "Core",
            "CoreUObject",
            "Engine",
            "Slate",
            "SlateCore",
            "UnrealEd",
            "ComponentVisualizers"
        });
    }
}

CustomModuleEd.h

#pragma once
#include "Modules/ModuleInterface.h"
#include "Modules/ModuleManager.h"

class FCustomModuleEd : public IModuleInterface
{
public:
    virtual void StartupModule() override;
    virtual void ShutdownModule() override;
};

CustomModuleEd.cpp

#include "CustomModuleEd.h"
#include "UnrealEdGlobals.h"
#include "Editor/UnrealEdEngine.h"

IMPLEMENT_GAME_MODULE(FCustomModuleEd, CustomModuleEd);

void FCustomModuleEd::StartupModule() {}
void FCustomModuleEd::ShutdownModule() {}

将编辑器模块添加到您的插件

编辑插件的 .uplugin 文件并添加新模块:

{
    "FileVersion": 3,
    ...
    "Modules": [
        {
            "Name": "CustomPlugin",
            "Type": "Runtime",
            "LoadingPhase": "Default"
        },
        {
            "Name": "CustomModuleEd",
            "Type": "Editor",
            "LoadingPhase": "PostEngineInit"
        }
    ]
}

注意: 将加载阶段设为 PostEngineInit。您可以在编辑器中通过 Tools → Debug → Modules 验证模块已加载。

添加调试器组件(运行时)

在你的 runtime 模块中创建一个虚拟组件,用作编辑器可视化器与想要显示的数据之间的中介。

UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class MYPROJECT_API UDebuggerComponent : public UActorComponent
{
    GENERATED_BODY()

public:
    // Sets default values for this component's properties
    UDebuggerComponent();

protected:
    // Called when the game starts
    virtual void BeginPlay() override;

public:
    // Called every frame
    virtual void TickComponent(float DeltaTime, ELevelTick TickType,
                               FActorComponentTickFunction* ThisTickFunction) override;

    /** Maximum distance at which debug geometry is drawn */
    UPROPERTY(EditAnywhere, Category = "Debug")
    float DrawDistance = 1000.0f;

    /** Toggle drawing of debug points */
    UPROPERTY(EditAnywhere, Category = "Debug")
    bool ShouldDrawDebugPoints = false;
};

该组件可以公开额外属性或读取其所属 Actor 的数据。它将附加到你希望可视化的任何 Actor 上。

添加可视化组件(编辑器)

editor 模块中创建一个继承自 FComponentVisualizer 的新类。由于该类不对其他模块公开,不需要 API 宏。

/**
 *  Visualizer for UDebuggerComponent.
 *  No API macro – we keep it editor‑only.
 */
class TestVisualizer : public FComponentVisualizer
{
public:
    /** Draws the debug visualization */
    virtual void DrawVisualization(const UActorComponent* Component,
                                  const FSceneView* View,
                                  FPrimitiveDrawInterface* PDI) override;

protected:
    /** Cached reference to the actor owning the component */
    AActor* ActorRef;

private:
    FVector PreviousCameraLocation;
};

实现

void TestVisualizer::DrawVisualization(const UActorComponent* Component,
                                        const FSceneView* View,
                                        FPrimitiveDrawInterface* PDI)
{
    const UDebuggerComponent* DebuggerComp = Cast(Component);
    if (!DebuggerComp || !DebuggerComp->ShouldDrawDebugPoints)
    {
        return;
    }

    // Example: draw a green point at the actor’s location
    PDI->DrawPoint(ActorRef->GetActorLocation(),
                   FLinearColor::Green,
                   25.0f,
                   SDPG_Foreground);
}

提示:UDebuggerComponent 附加到你想调试的任何 Actor 上,可以通过 Blueprint、在关卡中,或在 Actor 的构造函数中使用 CreateDefaultSubobject 以编程方式实现。

注册可视化器(编辑器启动)

最后,在编辑器模块的 StartupModule 中注册可视化器,并在 ShutdownModule 中将其清理。

void FCustomModuleEd::StartupModule()
{
    if (GUnrealEd)
    {
        // 创建并注册可视化器
        TSharedPtr<TestVisualizer> TestViz = MakeShareable(new TestVisualizer());
        GUnrealEd->RegisterComponentVisualizer(
            UDebuggerComponent::StaticClass()->GetFName(),
            TestViz);

        // 通知可视化器它已经被注册
        TestViz->OnRegister();
    }
}

void FCustomModuleEd::ShutdownModule()
{
    if (GUnrealEd)
    {
        // 模块关闭时注销可视化器
        GUnrealEd->UnregisterComponentVisualizer(
            UDebuggerComponent::StaticClass()->GetFName());
    }
}

现在,当你打开编辑器并选中包含 UDebuggerComponent 的 Actor 时,TestVisualizer 将根据组件的设置绘制调试点。

下面是相同内容的整理版本,保持了原始结构和含义。

// Example snippet (C++)
gerComponent::StaticClass()->GetFName());
}
}

进入全屏模式

退出全屏模式

现在编译并启动编辑器。当你选中已附加调试器组件的 Actor 时,PDI 将在类中的 DrawVisualization 函数里绘制你提到的调试可视化。

0 浏览
Back to Blog

相关文章

阅读更多 »

Unreal Engine 中的移动语义

拷贝问题 在传统的 C++98 编程中,对象的创建有两种方式:从零开始和通过拷贝。 cpp Foo; // 默认构造函数 Fooin...