To add animations to a sprite in Unity that's already using Spine, you don't necessarily have to re-export the entire project from Spine. Instead, you can manipulate the AnimationState
in Unity to add or change animations programmatically. The Spine-Unity runtime provides a robust API for controlling animations, including adding new animations to a track, blending animations, and more.
For example, to set a new animation, you can use the SetAnimation
method on the AnimationState
object associated with your Spine GameObject. Here's a basic example:
skeletonAnimation.AnimationState.SetAnimation(0, "new_animation_name", loop: true);
This code assumes you have a reference to the SkeletonAnimation
component named skeletonAnimation
and replaces the current animation on track 0 with "new_animation_name", setting it to loop.
If you're looking to add an animation on top of the existing one (e.g., to play a jump animation while running), you can use the AddAnimation
method instead:
skeletonAnimation.AnimationState.AddAnimation(0, "jump_animation", false, delay: 0);
This adds "jump_animation" to be played immediately after the current animation on track 0 without looping.
Remember, these methods require the animation names as strings, which must match exactly with the names used in your Spine project. If you're frequently changing animations, consider using AnimationReferenceAsset
for a more robust solution, as mentioned in the Spine-Unity documentation.
For more detailed control over animations, including event handling and mixing, refer to the Spine-Unity Runtime Documentation.