2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【UE4】 コンポーネントの情報を編集中に表示する方法

Last updated at Posted at 2016-03-04

コンポーネントビジュアライザー.gif

ゲームモジュール

HogeComponent.h
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class TTLEXTENSION_API UHogeComponent : public UActorComponent
{
	GENERATED_BODY()
public:	
	UHogeComponent();
	UPROPERTY(EditAnywhere, Category = "Variable")
	float Radius;
};

エディターモジュール

  • コンポーネントを表示する為のコード
HogeComponentVisualizer.h
#include "UnrealEd.h"
#include "ComponentVisualizer.h"
class FHogeComponentVisualizer
	: public FComponentVisualizer
{
public:
	virtual void DrawVisualization(const UActorComponent* Component, const FSceneView* View, FPrimitiveDrawInterface* PDI) override;

};
HogeComponentVisualizer.cpp
void FHogeComponentVisualizer::DrawVisualization(const UActorComponent* Component, const FSceneView* View, FPrimitiveDrawInterface* PDI)
{
	if (const UHogeComponent* Hoge = Cast<UHogeComponent>(Component))
	{
		if (AActor* Owner = Hoge->GetOwner())
		{
			// 球体を表示
			DrawWireSphere(PDI, Owner->GetActorLocation(), FColor::Yellow, Hoge->Radius, 16, SDPG_World);
		}
	}
}
  • コンポーネント表示を有効にする為のコード
モジュール.cpp
virtual void FHogeModule::StartupModule() override final
{
	if (GUnrealEd)
	{
		GUnrealEd->RegisterComponentVisualizer(UHogeComponent::StaticClass()->GetFName(), MakeShareable(new FHogeComponentVisualizer));
	}
}
	
void FHogeModule::ShutdownModule()
{
	if (GUnrealEd)
	{
		GUnrealEd->UnregisterComponentVisualizer(UHogeComponent::StaticClass()->GetFName());
	}
}
  • プロジェクトの読込タイミング
    UnrealEdが読み込まれるより早く、モジュールの開始が行われると、GUnrealEdがnullptrになってしまうためコンポーネントの表示が有効になりません。
    モジュールの読込タイミングのパラメータを"LoadingPhase": "PostEngineInit"にしてください
プロジェクト名.uproject
	"Modules": [

		{
			"Name": "エディターモジュール",
			"Type": "Developer",
			"LoadingPhase": "PostEngineInit"
		},
		(以下省略
	]
}

実行結果

ezgif-2708807382.gif

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?