LoginSignup
3
2

More than 1 year has passed since last update.

[UE4]Datasmith Runtimeでロード完了を検知してTransformを変更する

Last updated at Posted at 2022-03-03

はじめに

Unreal Datasmithファイルをロード直後に、任意のTransformを適用する方法です。

Datasmith RuntimeでDatasmithファイルをロードすると原点に3Dデータが表示されます。
位置・回転・サイズを任意に変更するにはロードが完了してからDatasmith Runtime ActorのTransformを変更する必要があります。

ロードが完了するまでにかかる時間は読み込むファイルサイズによって変わってくるので、Delay等で待つこともできません。
そこでロード完了を検知できるようにします。

Blueprintのみで実装する方法もありますが、やりたいことに合わなかったためC++も使って実装しています。

環境

  • Windows 10
  • Unreal Engine 4.27

手順

C++での実装箇所

ADatasmithRuntimeActorをC++で継承します。
OnImportEnd関数がロード完了時に呼ばれているようなので、オーバーライドしてイベントディスパッチャで通知するようにしました。

DatasmithRuntimeActorCustom.h
#pragma once

#include "CoreMinimal.h"
#include "DatasmithRuntime.h"
#include "DatasmithRuntimeActorCustom.generated.h"

UCLASS()
class TEST_API ADatasmithRuntimeActorCustom : public ADatasmithRuntimeActor
{
	GENERATED_BODY()
	
public:
	ADatasmithRuntimeActorCustom() : ADatasmithRuntimeActor(){}

	virtual void OnImportEnd() override;

	// Event Dispatcher[OnImportEndDispatcher]
	DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnImportEndDelegate);

	UPROPERTY(BlueprintAssignable, Category = "DatasmithRuntime")
	FOnImportEndDelegate OnImportEndDispatcher;

};
DatasmithRuntimeActorCustom.cpp
#include "DatasmithRuntimeActorCustom.h"

void ADatasmithRuntimeActorCustom::OnImportEnd()
{
	Super::OnImportEnd();

	// Call Event Dispathcer[OnImportEndDispatcher]
	OnImportEndDispatcher.Broadcast();
}

Blueprintでの実装箇所

BlueprintでのDatasmithファイルのロード処理で、下記のようにTransform変更処理をバインドします。
image.png

おまけ:Blueprintのみでやる方法

Collab Viewer Templateでは、下記の方法でロード完了を検知してTransformの変更を行っています。
但し、この方法では同じDatasmithファイルを読み込んだ時に無限ループに入ってしまったため、今回の方法を実装しました。
(※Collab Viewer Templateでは重複するファイルを読み込めないようになっています。)

※BP_DataSmith_Component ChangeLocation より引用、AnchorがDatasmithRuntimeActorのオブジェクト参照
image.png

参考

UE5から始める C++ & BP 12 【C++版】Event Dispatcher|ポジTA|note

3
2
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
3
2