Just in case some people would struggle like I did to unsubscribe a delegate function from Spine Event I figured I'd share this very simple piece of code :
// To keep track of your callback, it needs to be of Spine.AnimationState.CompleteDelegate type
Spine.AnimationState.CompleteDelegate callBack;
Spine.TrackEntry track;
void PlayAnimation (string pId, bool pLoop, System.Action pCallBack = null) {
// If there is already a callback subscribed, unsubscribe it
if (callBack != null) {
skeletonAnimation.state.Complete -= callBack ;
}
track = skeletonAnimation.state.SetAnimation(0, pId, pLoop);
// If pCallBack is specified, store it and subscribe it to Complete event
if (pCallBack != null) {
callBack = delegate { pCallBack(); }; // keeps a reference of the delegate
skeletonAnimation.state.Complete += callBack ;
}
}
Then you just have to call PlayAnimation with the function you want in parameter, and it will call it when the Animation is Complete. I find it extremely convenient and use it basically always. Of course this can be extended to handle different Event/Tracks etc.
I use that system because if you subscribe anonymous delegate to an Event, chances are you won't be able to unsubscribe it afterwards. Thus I found myself with hundred of delegate called upon each Complete event 🙂 It wasn't great.