Files
zeling_v2/Assets/Scripts/Animation/AnimationEventConfigSO.cs
2026-05-12 15:34:08 +08:00

65 lines
2.5 KiB
C#
Raw 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 System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace BaseGames.Animation
{
/// <summary>
/// 动画事件配置资产(架构 §AnimationModule
/// 为指定 AnimationClip 声明一组事件时机,由 AnimationEventBinder 在运行时注入 Animancer 回调。
///
/// 使用方式:
/// 1. 在 Inspector 中创建此资产菜单Animation/EventConfig
/// 2. 配置 TargetClip 和 Events 列表。
/// 3. 在 MonoBehaviour.Awake 中AnimationEventBinder.Bind(clip, config, this)。
/// </summary>
[CreateAssetMenu(menuName = "Animation/EventConfig")]
public class AnimationEventConfigSO : ScriptableObject
{
[Serializable]
public struct EventEntry
{
public AnimationEventType eventType;
[Range(0f, 1f)]
[Tooltip("事件在动画中的归一化时间0 = 开始1 = 结束)。")]
public float normalizedTime;
[Tooltip("附加字符串数据(可空)。用于 payload 参数。")]
public string data;
}
[Header("绑定的动画片段")]
public AnimationClip targetClip;
[Header("事件时机列表(归一化时间)")]
public EventEntry[] events;
/// <summary>
/// 用于编辑器工具验证:记录创建 Config 时 Clip 的实际帧数,
/// 若 Clip 被替换(长度漂移 &gt;5 帧)则 EventConfigEditor 显示警告。
/// </summary>
[HideInInspector] public float ExpectedClipLength = -1f;
// ── 运行时查询 ───────────────────────────────────────────────────
/// <summary>按归一化时间升序返回所有事件(保证 SetCallback 顺序稳定)。</summary>
public IEnumerable<EventEntry> SortedEvents =>
events != null
? events.OrderBy(e => e.normalizedTime)
: Enumerable.Empty<EventEntry>();
/// <summary>
/// 返回第一个匹配类型的归一化时间;未找到时返回 -1。
/// </summary>
public float GetNormalizedTime(AnimationEventType eventType)
{
if (events == null) return -1f;
foreach (var e in events)
if (e.eventType == eventType) return e.normalizedTime;
return -1f;
}
}
}