Ah, sorry. trackEntry has .Animation.duration
. It also has .endTime
which comes from .Animation.duration
. (But you can change endTime too.)
You're right. Your code is incorrect.
HandleEvent is only called if an event from the animation fires. It doesn't get called every frame.
I'll get back to you on Question 1 regarding how (best) to add events through code.
I expect you'll have to make similar calls to what the json/binary loader does.
But I expect that if you do add it through code that it's not easy to reorder events or remove one by one, and you would affect every single skeleton using that animation since Animations are not instanced per skeleton.
Checking per update is not inaccurate though, since you're just relying on trackEntry.time.
You noticed in the formula that you're not using an external (or any) deltaTime for comparison.
The truth is though that you'd probably consume less CPU cycles doing a time check than if you relied on the internal Events (the primary benefit is that it can be set by the animator, not that it's any faster). Either way, the CPU cost is not significant.
If you always know that there is a startCollider and endCollider event, you can key those in Spine editor, and then just modify the time of those keys at runtime. That will be a bit easier than adding those keys at runtime. But the same thing applies. All skeletons using that animation will be affected because that Animation is shared. And you cannot set them in the wrong order.
The other place you can hook up your check is in SkeletonAnimation.UpdateComplete
.
void Start ()
{
skeletonAnimation = GetComponent<SkeletonAnimation>();
skeletonAnimation.UpdateComplete += CheckTime;
}
void CheckTime (ISkeletonAnimation s)
{
var trackEntry = skeletonAnimation.state.GetCurrent(0);
// Do your checks here? if (startTime < trackEntry.time < endTime)
}