Files
zeling_v2/Assets/_Game/Scripts/Animation/AnimationEventBinder.cs

32 lines
1.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Animancer;
namespace BaseGames.Animation
{
/// <summary>
/// 静态工具类:将 AnimationEventConfigSO 中声明的事件注入 Animancer ClipTransition。
/// 使用 Animancer Pro APIClipTransition.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));
}
}
}
}