六一的部落格


关关难过关关过,前路漫漫亦灿灿。



Track Animation Notify


便签

-
AnimNotify类 动画通知
AnimSequenceBase类 动画

动画通知

通常在动画通知中, 对事件进行处理

实现委托机制, 在AnimNotify中定义委托, 添加委托对象, 在Notify函数中通知客户端

ShootThemUp: Animations/STUAnimNotify.h

 1DECLARE_MULTICAST_DELEGATE_OneParam(FOnNotifiedSignature, USkeletalMeshComponent*);
 2
 3UCLASS()
 4class SHOOTTHEMUP_API USTUAnimNotify : public UAnimNotify
 5{
 6    GENERATED_BODY()
 7
 8public:
 9    FOnNotifiedSignature OnNotified;
10    virtual void Notify(USkeletalMeshComponent *MeshComp, UAnimSequenceBase *Animation, const FAnimNotifyEventReference& EventReference) override;	
11};

ShootThemUp: Animation/STUAnimNotify.cpp

1void USTUAnimNotify::Notify(USkeletalMeshComponent *MeshComp, UAnimSequenceBase *Animation, const FAnimNotifyEventReference& EventReference)
2{
3    OnNotified.Broadcast(MeshComp);
4    Super::Notify(MeshComp, Animation, EventReference);
5}

在轨道中添加通知事件

以动画剪辑为例

  1. 在通知所属轨道的合适位置右键, 添加指定类型通知



  2. 动画剪辑播放到事件所在帧时, 调用UAnimNotify::Notify


订阅委托服务


遍历给定动画的通知事件, 获取指针, 指向指定类型的通知对象

ShootThemUp: Components/STUWeaponComponent.h

 1template<typename T>
 2T* FindFirstNotifyByClass(UAnimSequenceBase *Animation)
 3{
 4    if (!Animation) return nullptr;
 5
 6    const auto NotifyEvents = Animation->Notifies;
 7    for (auto NotifyEvent : NotifyEvents)
 8    {
 9        auto AnimNotify = Cast<T>(NotifyEvent.Notify);
10        if (AnimNotify)
11        {
12            return AnimNotify;
13        }
14    }
15    return nullptr;
16}

注册处理函数

ShootThemUp: Components/STUWeaponComponent.cpp

1auto EquipFinishedNotify = FindFirstNotifyByClass<USTUEquipFinishedAnimNotify>(EquipAnimMontage);
2if (EquipFinishedNotify)
3{
4    EquipFinishedNotify->OnNotified.AddUObject(this, &USTUWeaponComponent::OnEquipFinished);
5}

为轨道添加动画通知


Track Animation Notify


便签

-
AnimNotify类 动画通知
AnimSequenceBase类 动画

动画通知

通常在动画通知中, 对事件进行处理

实现委托机制, 在AnimNotify中定义委托, 添加委托对象, 在Notify函数中通知客户端

ShootThemUp: Animations/STUAnimNotify.h

 1DECLARE_MULTICAST_DELEGATE_OneParam(FOnNotifiedSignature, USkeletalMeshComponent*);
 2
 3UCLASS()
 4class SHOOTTHEMUP_API USTUAnimNotify : public UAnimNotify
 5{
 6    GENERATED_BODY()
 7
 8public:
 9    FOnNotifiedSignature OnNotified;
10    virtual void Notify(USkeletalMeshComponent *MeshComp, UAnimSequenceBase *Animation, const FAnimNotifyEventReference& EventReference) override;	
11};

ShootThemUp: Animation/STUAnimNotify.cpp

1void USTUAnimNotify::Notify(USkeletalMeshComponent *MeshComp, UAnimSequenceBase *Animation, const FAnimNotifyEventReference& EventReference)
2{
3    OnNotified.Broadcast(MeshComp);
4    Super::Notify(MeshComp, Animation, EventReference);
5}

在轨道中添加通知事件

以动画剪辑为例

  1. 在通知所属轨道的合适位置右键, 添加指定类型通知



  2. 动画剪辑播放到事件所在帧时, 调用UAnimNotify::Notify


订阅委托服务


遍历给定动画的通知事件, 获取指针, 指向指定类型的通知对象

ShootThemUp: Components/STUWeaponComponent.h

 1template<typename T>
 2T* FindFirstNotifyByClass(UAnimSequenceBase *Animation)
 3{
 4    if (!Animation) return nullptr;
 5
 6    const auto NotifyEvents = Animation->Notifies;
 7    for (auto NotifyEvent : NotifyEvents)
 8    {
 9        auto AnimNotify = Cast<T>(NotifyEvent.Notify);
10        if (AnimNotify)
11        {
12            return AnimNotify;
13        }
14    }
15    return nullptr;
16}

注册处理函数

ShootThemUp: Components/STUWeaponComponent.cpp

1auto EquipFinishedNotify = FindFirstNotifyByClass<USTUEquipFinishedAnimNotify>(EquipAnimMontage);
2if (EquipFinishedNotify)
3{
4    EquipFinishedNotify->OnNotified.AddUObject(this, &USTUWeaponComponent::OnEquipFinished);
5}