EventTimelineExtensions.cs contains a static class with a set of convenience methods/extensions that help the user create or modify Spine.EventTimelines
at runtime.
Sample usage in Unity:
EventTimelineSample.cs
using UnityEngine;
using System.Collections.Generic;
using Spine;
using Spine.Unity;
public class EventTimelineSample : MonoBehaviour {
void Start () {
var skeletonAnimation = this.GetComponent<Spine.Unity.SkeletonAnimation>();
Spine.SkeletonData skeletonData = skeletonAnimation.skeleton.data;
// Create EventData objects.
Spine.EventData myEventABC = new Spine.EventData("ABC");
Spine.EventData myEventDEF = new Spine.EventData("DEF");
// Add EventData to SkeletonData. Not necessary but good if you want to keep your SkeletonData together or share with other instances.
// skeletonData.events.Add(myEventABC);
// skeletonData.events.Add(myEventDEF);
// EventData can also be pre-existing from what you added in Spine editor.
//Spine.EventData myExistingEventData = skeletonData.FindEvent("my event I added in Spine");
// Make a temporary list to be converted into an EventTimeline later.
List<Spine.Event> myEventList = new List<Spine.Event>();
myEventList.Add(new Spine.Event(0.5f, myEventABC)); // fire myEventABC at 0.5 seconds.
myEventList.Add(new Spine.Event(1.0f, myEventDEF)); // fire myEventDEF at 1 second.
myEventList.Add(new Spine.Event(2.0f, myEventDEF)); // fire myEventDEF again at 2 seconds.
//myEventList.Add(new Spine.Event(3.0f, myExistingEventData)); // fire myExistingEventData at 3 seconds.
// Convert the temporary list into an EventTimeline and add it to your Spine.Animation.
Spine.EventTimeline myNewEventTimeline = myEventList.ToEventTimeline(); // This is an extension method.
// Find the animation you want to add the event timeline to and add the new timeline to it.
Spine.Animation myAnimation = skeletonData.FindAnimation("my run animation");
myAnimation.Timelines.Add(myNewEventTimeline);
// Test the animation with the added events.
skeletonAnimation.state.SetAnimation(0, myAnimation, true);
skeletonAnimation.state.Event += (state, trackIndex, e) => Debug.Log(e.Data.name + " fired at " + Time.time);
}
}