using Animancer;
namespace BaseGames.Animation
{
///
/// 静态工具类:将 AnimationEventConfigSO 中声明的事件注入 Animancer ClipTransition。
/// 使用 Animancer Pro API:ClipTransition.Events.SetCallback(normalizedTime, Action)。
/// 调用时机:MonoBehaviour.Awake(在 Animancer 播放前完成绑定)。
///
public static class AnimationEventBinder
{
///
/// 将 config 中的所有事件回调注入到 clip 的 Animancer 事件系统。
///
/// 目标 Animancer ClipTransition。
/// 事件配置资产(null 时静默跳过)。
/// 事件接收者(通常是同一 MonoBehaviour)。
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));
}
}
}
}