当前位置: 首页 > java >正文

UE5 为啥原生的NotifyState写逻辑会有问题

UE5 为啥原生的NotifyState写逻辑会有问题

虚幻的UAnimNotifyState,也就是Montage中配置的通知条存在明显的问题,请勿用来写逻辑,做表现是可以的。

UE5.3 相关源码:

void UAnimInstance::TriggerAnimNotifies(float DeltaSeconds)
{SCOPE_CYCLE_COUNTER(STAT_AnimTriggerAnimNotifies);USkeletalMeshComponent* SkelMeshComp = GetSkelMeshComponent();// Array that will replace the 'ActiveAnimNotifyState' at the end of this function.TArray<FAnimNotifyEvent> NewActiveAnimNotifyState;NewActiveAnimNotifyState.Reserve(NotifyQueue.AnimNotifies.Num());TArray<FAnimNotifyEventReference> NewActiveAnimNotifyEventReference;NewActiveAnimNotifyEventReference.Reserve(NotifyQueue.AnimNotifies.Num());// AnimNotifyState freshly added that need their 'NotifyBegin' event called.TArray<const FAnimNotifyEvent *> NotifyStateBeginEvent;TArray<const FAnimNotifyEventReference *> NotifyStateBeginEventReference;for (int32 Index=0; Index<NotifyQueue.AnimNotifies.Num(); Index++){if(const FAnimNotifyEvent* AnimNotifyEvent = NotifyQueue.AnimNotifies[Index].GetNotify()){// AnimNotifyStateif (AnimNotifyEvent->NotifyStateClass){int32 ExistingItemIndex = INDEX_NONE;if (ActiveAnimNotifyState.Find(*AnimNotifyEvent, ExistingItemIndex)){check(ActiveAnimNotifyState.Num() == ActiveAnimNotifyEventReference.Num());ActiveAnimNotifyState.RemoveAtSwap(ExistingItemIndex, 1, false); ActiveAnimNotifyEventReference.RemoveAtSwap(ExistingItemIndex, 1, false);}else{NotifyStateBeginEvent.Add(AnimNotifyEvent);NotifyStateBeginEventReference.Add(&NotifyQueue.AnimNotifies[Index]);}NewActiveAnimNotifyState.Add(*AnimNotifyEvent);FAnimNotifyEventReference& EventRef = NewActiveAnimNotifyEventReference.Add_GetRef(NotifyQueue.AnimNotifies[Index]);EventRef.SetNotify(&NewActiveAnimNotifyState.Top());continue;}// Trigger non 'state' AnimNotifiesTriggerSingleAnimNotify(NotifyQueue.AnimNotifies[Index]);}}// Send end notification to AnimNotifyState not active anymore.for (int32 Index = 0; Index < ActiveAnimNotifyState.Num(); ++Index){const FAnimNotifyEvent& AnimNotifyEvent = ActiveAnimNotifyState[Index];const FAnimNotifyEventReference& EventReference = ActiveAnimNotifyEventReference[Index];if (AnimNotifyEvent.NotifyStateClass && ShouldTriggerAnimNotifyState(AnimNotifyEvent.NotifyStateClass)){
#if WITH_EDITOR// Prevent firing notifies in animation editors if requested if(!SkelMeshComp->IsA<UDebugSkelMeshComponent>() || AnimNotifyEvent.NotifyStateClass->ShouldFireInEditor())
#endif{TRACE_ANIM_NOTIFY(this, AnimNotifyEvent, End);AnimNotifyEvent.NotifyStateClass->NotifyEnd(SkelMeshComp, Cast<UAnimSequenceBase>(AnimNotifyEvent.NotifyStateClass->GetOuter()), EventReference);}}// The NotifyEnd callback above may have triggered actor destruction and the tear down// of this instance via UninitializeAnimation which empties ActiveAnimNotifyState.// If that happened, we should stop iterating the ActiveAnimNotifyState arrayif (ActiveAnimNotifyState.IsValidIndex(Index) == false){ensureMsgf(false, TEXT("UAnimInstance::ActiveAnimNotifyState has been invalidated by NotifyEnd. AnimInstance: %s, Owning Component: %s, Owning Actor: %s "), *GetNameSafe(this), *GetNameSafe(GetOwningComponent()), *GetNameSafe(GetOwningActor()));return;}}check(NotifyStateBeginEventReference.Num() == NotifyStateBeginEvent.Num());for (int32 Index = 0; Index < NotifyStateBeginEvent.Num(); Index++){const FAnimNotifyEvent* AnimNotifyEvent = NotifyStateBeginEvent[Index];const FAnimNotifyEventReference * AnimNotifyEventReference = NotifyStateBeginEventReference[Index];if (ShouldTriggerAnimNotifyState(AnimNotifyEvent->NotifyStateClass)){
#if WITH_EDITOR// Prevent firing notifies in animation editors if requested if(!SkelMeshComp->IsA<UDebugSkelMeshComponent>() || AnimNotifyEvent->NotifyStateClass->ShouldFireInEditor())
#endif{TRACE_ANIM_NOTIFY(this, *AnimNotifyEvent, Begin);AnimNotifyEvent->NotifyStateClass->NotifyBegin(SkelMeshComp, Cast<UAnimSequenceBase>(AnimNotifyEvent->NotifyStateClass->GetOuter()), AnimNotifyEvent->GetDuration(), *AnimNotifyEventReference);}}}// Switch our arrays.ActiveAnimNotifyState = MoveTemp(NewActiveAnimNotifyState);ActiveAnimNotifyEventReference = MoveTemp(NewActiveAnimNotifyEventReference);// Tick currently active AnimNotifyStatefor (int32 Index = 0; Index < ActiveAnimNotifyState.Num(); Index++){const FAnimNotifyEvent& AnimNotifyEvent = ActiveAnimNotifyState[Index];const FAnimNotifyEventReference& EventReference = ActiveAnimNotifyEventReference[Index];if (ShouldTriggerAnimNotifyState(AnimNotifyEvent.NotifyStateClass)){
#if WITH_EDITOR// Prevent firing notifies in animation editors if requested if(!SkelMeshComp->IsA<UDebugSkelMeshComponent>() || AnimNotifyEvent.NotifyStateClass->ShouldFireInEditor())
#endif{TRACE_ANIM_NOTIFY(this, AnimNotifyEvent, Tick);AnimNotifyEvent.NotifyStateClass->NotifyTick(SkelMeshComp, Cast<UAnimSequenceBase>(AnimNotifyEvent.NotifyStateClass->GetOuter()), DeltaSeconds, EventReference);}}}
}

问题一:同帧下的Begin 与End,它是按For循环执行的,没有按照时间顺序执行。如下图的NotifyStateA与NotifyStateB,他们在第N帧的时候,A的End会要比B的Start先执行,但是在时间的顺序上,B的Begin时间是早于A的End的,如果依赖时间顺序去执行,某些代码逻辑时候,当卡的时候,逻辑必有问题。
在这里插入图片描述
问题二:它没有上下执行顺序的问题,并不是同一时间下上面的先执行。
问题三:同一帧里面有NotifyState的Start 与 End,那么End会在下一帧执行,而不是同帧下执行
在这里插入图片描述

http://www.xdnf.cn/news/19560.html

相关文章:

  • 开源低代码平台(NocoBase)
  • 20250828的学习笔记
  • 9.1日IO作业
  • 2025年09月01日Github流行趋势
  • 99、23种设计模式之组合模式(8/23)
  • 09.《路由基础知识解析和实践》
  • 基于外部对照数据借用的临床试验统计分析方案设计与仿真研究
  • PitVis-2023挑战赛:内镜下垂体瘤手术视频中的手术流程识别|文献速递-深度学习人工智能医疗图像
  • 如何把指定阿里云文件夹下的所有文件移动到另一个文件夹下,移动文件时把文件名称(不包括文件后缀)进行md5编码
  • 从理论到实践,深入剖析数据库水平拆分的安全平滑落地
  • Spark自定义累加器实现高效WordCount
  • Spark和Spring整合处理离线数据
  • promptoMANIA-AI绘画提示词生成器
  • Electron使用WebAssembly实现CRC-16 CCITT校验
  • macOS中Homebrew安装PHP的详细步骤(五)
  • 深入了解Flink核心:Slot资源管理机制
  • PostgreSQL 索引大全
  • 深入理解Docker容器技术:原理与实践
  • 如何安装CUDA????
  • three.js+WebGL踩坑经验合集(10.1):镜像问题又一坑——THREE.InstancedMesh的正反面显示问题
  • 机器学习-时序预测2
  • 基于FPGA+DSP数据采集处理平台的搭建
  • 【Vue2 ✨】Vue2 入门之旅(四):生命周期钩子
  • Unity核心概念③:Inspector窗口可编辑变量
  • C++/QT day3(9.1)
  • 深度学习中常用的激活函数
  • 关系型数据库——GaussDB的简单学习
  • Spring Boot 和 Spring Cloud 的原理和区别
  • 对于牛客网—语言学习篇—编程初学者入门训练—复合类型:BC141 井字棋及BC142 扫雷题目的解析
  • Composefile配置