多轮审查和修复
This commit is contained in:
31
Assets/Scripts/Animation/AnimationEventBinder.cs
Normal file
31
Assets/Scripts/Animation/AnimationEventBinder.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using Animancer;
|
||||
|
||||
namespace BaseGames.Animation
|
||||
{
|
||||
/// <summary>
|
||||
/// 静态工具类:将 AnimationEventConfigSO 中声明的事件注入 Animancer ClipTransition。
|
||||
/// 使用 Animancer Pro API:ClipTransition.Events.SetCallback(normalizedTime, Action)。
|
||||
/// 调用时机:MonoBehaviour.Awake(在 Animancer 播放前完成绑定)。
|
||||
/// </summary>
|
||||
public static class AnimationEventBinder
|
||||
{
|
||||
/// <summary>
|
||||
/// 将 config 中的所有事件回调注入到 clip 的 Animancer 事件系统。
|
||||
/// </summary>
|
||||
/// <param name="clip">目标 Animancer ClipTransition。</param>
|
||||
/// <param name="config">事件配置资产(null 时静默跳过)。</param>
|
||||
/// <param name="receiver">事件接收者(通常是同一 MonoBehaviour)。</param>
|
||||
public static void Bind(ClipTransition clip, AnimationEventConfigSO config, IAnimationEventHandler receiver)
|
||||
{
|
||||
if (clip == null || config == null || receiver == null) return;
|
||||
|
||||
foreach (var entry in config.SortedEvents)
|
||||
{
|
||||
// 捕获循环变量,避免闭包陷阱
|
||||
var captured = entry;
|
||||
clip.Events.Add(captured.normalizedTime, () =>
|
||||
receiver.HandleEvent(captured.eventType, captured.data));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user