How do I get all the animationstates of animator?I found No api

I want to add some events to some clips at runtime, but no way to find em.

@TonyLi found a way in this post:

not sure if it still works in the latest version, also not sure if that is a runtime or editor script… haven’t checked. But you’re right, doesn’t appear to be documented.

It’s editor only. At runtime, I think @Mecanim-Dev would instead recommend using animator state tags. You can query the current state’s tag using IsTag(), but I don’t know if Mecanim has a way yet to enumerate all tags.

Huh, I never even noticed the Tags. I’ll have to experiment with that.

Would Tag compares be preferred versus doing something like

 while (_thisAnimator.GetCurrentAnimatorStateInfo(0).shortNameHash == hashCache) yield return null;

?

In that example, I guess it’s just personal preference. With tags you don’t need to store hashCache.

Mecanim.Dev provided another good example for tags. You could tag your combat-related animation states with the same tag. When the animator is in a state with that tag, you could crescendo your battle music. This would be easier than comparing the current state against a list of hashCaches.

2 Likes

Good example, thanks.