SpinePro中添加事件帧
首先 选中右上角的层级树 然后选择事件选项
最后在右下角看到 新建 点击它 新建一个事件
点击左上角的设置按钮 弹出编辑窗口
编辑窗口
在右上角 动画栏 可以切换对应的动画
点坐边的那个小灰点来切换 亮点代表当前动画
选中帧
添加事件 点击对应事件的图标 在当前帧添加事件
添加成功后 点击左下角播放按钮 可以看到 人物身边有对应事件的文本弹出 表示添加成功
Unity监听Spine帧事件
面板上添加
可以一个事件 添加多个监听
代码中动态添加
一个事件可以添加多个监听
源码
using System.Collections.Generic;
using UnityEngine;
using Spine.Unity;
using Spine;
using System;
using UnityEngine.Events;
public class SpineEventHelper : MonoBehaviour
{
[Serializable]
public class SpineFrameKey
{
[SpineEvent] public string key;
public UnityEvent handler;
}
[SerializeField] private List<SpineFrameKey> handlers;
private Dictionary<string, UnityEvent> keyFrameEventDict = new Dictionary<string, UnityEvent>( );
public const string START_EVENT = "START_EVENT", END_EVENT = "END_EVENT";
private SkeletonAnimation skeletonAnimation;
private void Start( )
{
Bind( );
}
private void Bind( )
{
skeletonAnimation = GetComponent<SkeletonAnimation>( );
if ( skeletonAnimation == null ) return;
skeletonAnimation.AnimationState.Event += HandleEvent;
skeletonAnimation.AnimationState.Start += delegate ( TrackEntry trackEntry )
{
if ( keyFrameEventDict.TryGetValue( START_EVENT, out UnityEvent @event ) )
{
@event.Invoke( );
}
};
skeletonAnimation.AnimationState.End += delegate
{
if ( keyFrameEventDict.TryGetValue( END_EVENT, out UnityEvent @event ) )
{
@event.Invoke( );
}
};
foreach ( var handler in handlers )
{
keyFrameEventDict[ handler.key ] = handler.handler;
}
}
public void AddCustomEventHandler( string key, UnityAction handler )
{
if ( keyFrameEventDict.TryGetValue( key, out UnityEvent ev ) )
{
ev.AddListener( handler );
}
else
{
var ue = new UnityEvent( );
ue.AddListener( handler );
keyFrameEventDict.Add( key, ue );
}
}
public void RemoveCustomEventHandler( string key, UnityAction handler = null )
{
if ( keyFrameEventDict.TryGetValue( key, out UnityEvent ev ) )
{
if ( handler == null )
{
ev.RemoveAllListeners( );
}
else
{
ev.RemoveListener( handler );
}
}
}
public void AddStartEventHandler( UnityAction handler )
{
if ( keyFrameEventDict.TryGetValue( START_EVENT, out UnityEvent ev ) )
{
ev.AddListener( handler );
}
else
{
var ue = new UnityEvent( );
ue.AddListener( handler );
keyFrameEventDict.Add( START_EVENT, ue );
}
}
public void RemoveStartEventHandler( UnityAction handler )
{
if ( keyFrameEventDict.TryGetValue( START_EVENT, out UnityEvent ev ) )
{
ev.RemoveListener( handler );
}
}
public void AddEndEventHandler( UnityAction handler )
{
if ( keyFrameEventDict.TryGetValue( END_EVENT, out UnityEvent ev ) )
{
ev.AddListener( handler );
}
else
{
var ue = new UnityEvent( );
ue.AddListener( handler );
keyFrameEventDict.Add( END_EVENT, ue );
}
}
public void RemoveEndEventHandler( UnityAction handler )
{
if ( keyFrameEventDict.TryGetValue( END_EVENT, out UnityEvent ev ) )
{
ev.RemoveListener( handler );
}
}
private void HandleEvent( TrackEntry trackEntry, Spine.Event e )
{
if ( keyFrameEventDict.TryGetValue( e.Data.Name, out UnityEvent @event ) )
{
@event.Invoke( );
}
}
public void Clear( )
{
keyFrameEventDict.Clear( );
}
private void OnDestroy( )
{
Clear( );
}
}