For anyone coming here and getting confused, here's a solution that might work! 🙂
The lastTime and time parameters have been renamed since this post was made. By looking at the docs, I figured these parameters would be correct, and I think I figured it out!
Here's what I tried:
// targetTime is a float, defined in the class
animationBackwards = true; // I have this enabled just to force it to play backwards while debugging.
if (animationBackwards) {
currentMovementTrack.timeScale = 0f; // so time isn't moved forward by SkeletonAnimation/AnimationState
currentMovementTrack.animationLast = 0f; // this may cause multiple events to fire if you have those.
currentMovementTrack.trackTime = targetTime; // you should decrement targetTime by a delta time (to act as the animation time cursor that moves backwards.)
skeletonAnim.state.Apply(skeletonAnim.skeleton); // may be required, depending on project script execution order settings. It would be more efficient if this script runs before the SkeletonAnimation.cs and you won't need this.
targetTime -= Time.deltaTime;
if (targetTime <= 0) targetTime = currentMovementTrack.animationEnd; // You need to make sure the trackTime is within the limits of the animation, or will not work
} else {
currentMovementTrack.timeScale = 1f;
}
I still need to figure out how to change the speed, since I do that with currentMovementTrack.timeScale normally, but I guess I would multiple the animation speed with Time.deltaTime.
The use case for this is my character can aim a gun to the right, and walk to the left, and the animation should be playing backwards.
Like others have said, it's not practical to duplicate all animations to go in two directions, since any adjustments to the orignal animation would require a lot of work.
I can see that it might not be possible to play an animation backwards and maintain all features, but it's a bit unwieldy to have to do it this way, and it would be practical if it was supported by the API.
I would probably like to have events on footsteps down, but didn't try that yet, and multiple events firing when playing backwards doesn't seem very good, but we'll see. 🙂
Hope this helps anyone coming here 🙂