PayasoPrince

Good Evening,

I don't like using strings, so I am using:
public SkeletonAnimation SpineSkeleton;
public AnimationReferenceAsset Idle;
public AnimationReferenceAsset Walk;
I'm able to play animations fine with:
SpineSkeleton.state.SetAnimation(0, animation, true);
(Should this always be 0 if I never have a queue of animations?)

But I want to do a check to see if that animation is already playing.
Example:
If( "Animation currently playing " != Walk) { // Do stuff}
I just need ^ "Animation currently playing. How can I reference this?

Thank you! :)
User avatar
PayasoPrince
  • Posts: 38

Misaki

But I want to do a check to see if that animation is already playing.
Example:
If( "Animation currently playing " != Walk) { // Do stuff}
I just need ^ "Animation currently playing. How can I reference this?
You can use AnimationState getCurrent to get the track entry for the animation currently playing on the track. (It returns null if no animation is currently playing.)
Fortunately, there are an example scene and a C# script to show you how to check the current animation on a specific track and do something depending on that. You can find the scene Spine Examples/Getting Started/5 Basic Platformer, and SkeletonAnimationHandleExample.cs which is attached to the Player GameObject in the scene shows you the example code.
(Should this always be 0 if I never have a queue of animations?)
I’m afraid to say but I’m not sure what you mean by that. What is it that you are concerned about?
User avatar
Misaki

Misaki
  • Posts: 1164

PayasoPrince

Misaki wrote:You can use AnimationState getCurrent to get the track entry for the animation currently playing on the track. (It returns null if no animation is currently playing.)
Fortunately, there are an example scene and a C# script to show you how to check the current animation on a specific track and do something depending on that. You can find the scene Spine Examples/Getting Started/5 Basic Platformer, and SkeletonAnimationHandleExample.cs which is attached to the Player GameObject in the scene shows you the example code.
Hey, thanks for the reply.

I didn't want the example scenes to take up space in my project. So I don't have the example scene unfortunately.

I just ended up doing this:
public SkeletonAnimation SpineSkeleton;
private AnimationReferenceAsset currentState;
public AnimationReferenceAsset Idle;
public AnimationReferenceAsset Walk;

// Start is called before the first frame update
void Start()
{

}

public void SetAnimation(AnimationReferenceAsset animation)
{
if (CheckIfAlreadyPlaying(animation))
{
SpineSkeleton.state.SetAnimation(0, animation, true);
}

}

private bool CheckIfAlreadyPlaying(AnimationReferenceAsset animation)
{
if ( currentState != animation)
{
currentState = animation;
return true;
}

else
return false;
}
But I'd like to know how to use it properly anyway. It doesn't look like you can use AnimationState getCurrent to compare it with an AnimationReferenceAsset. If you could provide me an example of how you would use AnimationState getCurrent to see if it's the same as a specific animation, that would be great.
Misaki wrote:I’m afraid to say but I’m not sure what you mean by that. What is it that you are concerned about?
When will the first parameter in SetAnimation() not be 0? I've just been setting it to 0 every time to play animations.

Thank you!
User avatar
PayasoPrince
  • Posts: 38

Misaki

When will the first parameter in SetAnimation() not be 0? I've just been setting it to 0 every time to play animations.
The first parameter is track index, and you can set an animation on any track you like by specifying the track index. If you are not familiar with tracks, the following tutorial video starting from 03:02 would be helpful:


The video also shows the example code using the GetCurrent() method from 13:46 to 14:11, but it is a bit complex and I believe it is better to see the actual script. If you do not want to add the examples to your project, create a new Unity project and install the spine-unity runtime to check them out.
User avatar
Misaki

Misaki
  • Posts: 1164

Harald

PayasoPrince wrote:I didn't want the example scenes to take up space in my project. So I don't have the example scene unfortunately.
Then create a second project with just the example scenes in it (or ask a fellow programmer to help you with that). If you're limited by disk space on your harddrives, hardware stores also sell external harddisks ;).

Alternatively you can just browse the files via the github web front-end:
https://github.com/EsotericSoftware/spine-runtimes/tree/4.1/spine-unity/Assets/Spine%20Examples

If you never took a look at the example scenes, we would highly recommend doing so as many questions might be answered by them.
PayasoPrince wrote:It doesn't look like you can use AnimationState getCurrent to compare it with an AnimationReferenceAsset.
Sure, just access AnimationReferenceAsset.Animation.
User avatar
Harald

Harri
  • Posts: 4459

PayasoPrince

Misaki wrote:You can find the scene Spine Examples/Getting Started/5 Basic Platformer, and SkeletonAnimationHandleExample.cs which is attached to the Player GameObject in the scene shows you the example code.
This was helpful. Thank you!
User avatar
PayasoPrince
  • Posts: 38


Return to Unity