帧事件 辅助脚本
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimatorListener : MonoBehaviour
{
private Dictionary<string, Action> handlerDict = new Dictionary<string, Action>();
/// <summary>
///
/// 帧事件监听
///
/// </summary>
/// <param name="eventId"></param>
public void On( string eventId, Action handler )
{
if ( handlerDict.ContainsKey( eventId ) )
{
handlerDict[ eventId ] += handler;
}
else
{
handlerDict.Add( eventId, handler );
}
}
/// <summary>
/// 帧事件移除
/// </summary>
/// <param name="eventId"></param>
/// <param name="handler"></param>
public void Off( string eventId, Action handler )
{
if ( handlerDict.ContainsKey( eventId ) )
{
handlerDict[ eventId ] -= handler;
}
}
/// <summary>
/// 清除所有的帧事件
/// </summary>
public void Clear()
{
handlerDict.Clear();
}
/// <summary>
/// 触发帧事件
/// </summary>
/// <param name="eventId"></param>
public void OnAnimatorEvent( string eventId )
{
Action result = null;
if ( handlerDict.TryGetValue( eventId, out result ) )
{
result?.Invoke();
}
}
}
使用方法
添加到有 Animator组件的对象上
然后给动画文件添加帧事件
添加一个动画事件关键帧 方法名保持统一 OnAnimatorEvent 字符串属性填 skill
脚本获取到 AnimatorListener组件
监听帧事件
事件移除
移除全部事件监听
移除指定