• Unity
  • Check if animation track is empty?

Related Discussions
...

How do I check if a track is empty?

This is for an idle mode, i want to set track 3 to empty, if it's not empty.
If it's empty, don't do anything.

SetAnim with parameter null calls SetEmptyAnimation.

if (skeletonAnimation.state.GetCurrent(3) != null) {
   Debug.Log(Time.frameCount + ": Track 3 is not null, has anim = " + skeletonAnimation.state.GetCurrent(3).Animation.Name);
   SetAnim(3, null, mix: 0.2f, loop: false, allowReset: true, timeScale: 1);
}

I get:

439: Track 3 is not null, has anim = <empty>

If I use this code I get a nullref on the second check in the if statement.

if (skeletonAnimation.state.GetCurrent(3) != null || skeletonAnimation.state.GetCurrent(3).Animation.Name != "<empty>") {
   Debug.Log(Time.frameCount + ": Track 3 is not null, has anim = " + skeletonAnimation.state.GetCurrent(3).Animation.Name);
   SetAnim(3, null, mix: 0.2f, loop: false, allowReset: true, timeScale: 1);
}

So how do I check if an animation track is empty?

Thanks!

nicmar wrote
if (skeletonAnimation.state.GetCurrent(3) != null || skeletonAnimation.state.GetCurrent(3).Animation.Name != "<empty>") {

You are basically doing the following here:

TrackEntry entry = skeletonAnimation.state.GetCurrent(3);
if (entry != null || // this will early-out when it's != null, otherwise continue to the next line:
    entry.AnimationName) // here entry is guaranteed to be null, so why are you dereferencing any member vars here? ;)

It helps readability to create local shorter-named vars instead of using long copy-paste lines, which (mostly) unnecessarily re-evaluate the same statement. 😉