C++でアニメーション通知(Animation Notifications)の「AnimNotifyState」を作成する方法です。
こちらを参考にさせていただきました。
・UE4 AnimNotifyをC++で書く場合のメモhttps://qiita.com/unknown_ds/items/ecadbe7b76cb3b1835d9公式ドキュメントはこちら。
・UAnimNotifyStatehttps://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/Animation/AnimNotifies/UAnimNotifyState/アニメーション通知(Animation Notifications)の「AnimNotifyState」を作るには
「UAnimNotifyState」を継承したクラスを作成します。
まずはヘッダの定義になります。
#include "Animation/AnimNotifies/AnimNotifyState.h"
// AnimNotifyStateクラス
class UAnimNotifyState_New : public UAnimNotifyState
{
GENERATED_BODY()
public:
// コンストラクタ
UAnimNotifyState_New ();
public:// Notify 設定
virtual FString GetNotifyName_Implementation() const override;
virtual void NotifyBegin(USkeletalMeshComponent* MeshComp, UAnimSequenceBase * Animation, float TotalDuration, const FAnimNotifyEventReference& EventReference);
virtual void NotifyEnd(USkeletalMeshComponent* MeshComp, UAnimSequenceBase * Animation, const FAnimNotifyEventReference& EventReference);
virtual void NotifyTick(USkeletalMeshComponent* MeshComp, UAnimSequenceBase * Animation, float FrameDeltaTime, const FAnimNotifyEventReference& EventReference);
public:// 公開変数
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int32 Value;
};
上記で
「UAnimNotifyState」を継承したクラスを作成しました。
・
「GetNotifyName_Implementation」関数は「Notify」を配置した場合に表示される文字列を記述します。
・「NotifyBegin」関数は「Notify」が開始した時の処理を記述します。
・「NotifyEnd」関数は「Notify」が終了した時の処理を記述します。
・「NotifyTick」関数は「Notify」が実行中の処理を記述します。
・変数を定義することで「Notify」のパラメータとして受け取ることができます。
次に実際の処理を記述します。
・通知が開始された時の処理(NotifyBegin)// 通知が開始された時の処理
void UAnimNotifyState_New ::NotifyBegin(USkeletalMeshComponent* MeshComp, UAnimSequenceBase* Animation, float TotalDuration, const FAnimNotifyEventReference& EventReference)
{
// 取得
TObjectPtr< AActor> aActor = Cast< AActor>(MeshComp->GetOwner());
// 何か処理色々
}
・通知が終了した時の処理(NotifyEnd)// 通知が終了した時の処理
void UAnimNotifyState_New ::NotifyEnd(USkeletalMeshComponent* MeshComp, UAnimSequenceBase* Animation, const FAnimNotifyEventReference& EventReference)
{
// 取得
TObjectPtr< AActor> aActor = Cast< AActor>(MeshComp->GetOwner());
// 何か処理色々
}
・通知が実行中の処理(NotifyTick)// 通知が実行中の処理
void UAnimNotifyState_New ::NotifyTick(USkeletalMeshComponent* MeshComp, UAnimSequenceBase* Animation, float FrameDeltaTime, const FAnimNotifyEventReference& EventReference)
{
// 取得
TObjectPtr< AActor> aActor = Cast< AActor>(MeshComp->GetOwner());
// 何か処理色々
}
こんな感じで通知がきたときの処理を書くことができます。
簡単な通知はBPで作ってしまったほうが良い場合もありますが、
ある程度複雑な処理の場合はC++で書いた方が楽なのかなと思いました。