- Home /
How to get a reference of "Animations" array?
Hello,
I'm trying to figure out how to get a reference to the "Animations" array that the Animation component holds onto.
You can set this up in the Editor to hold more than just your default clip, and you can gain access to these clips in a variety of ways. You can animation.GetClipCount to find out how many are in there, you can play an animation directly by inputting a string of the animation's name: animation.Play("NewAnimation"), but I cannot figure out how to access the animation clip simply by it's position in the array.
I can't seem to find any reference as to how to get to the "Animations" array nor how to play a clip by any method besides using a string or adding the clip to the array.
Thanks!
Answer by Mike 3 · May 07, 2011 at 08:06 PM
Generally you iterate through the animation enumerable to get at the AnimationState objects:
foreach(AnimationState state in animation)
{
//do something with state
}
You could also add them all into a list fairly easily, using linq to cast the enumerable (or use an arraylist instead i guess)
List<AnimationState> states = new List<AnimationState>(animation.Cast<AnimationState>());
or
ArrayList states = new ArrayList(animation);
From the state, you can get at the name for playing, or even grab the clip it refers to (.name and .clip respectively)
5 years old trick and still works... If you use just
foreach(AnimationState state in animation)
{
//do something with state
}
there are various surprises co$$anonymous$$g, like "cannot cast from source to destination" or the last state name is somehow the same as the first one. The correct solution seems to be:
List<AnimationState> states = new List<AnimationState>(animation.Cast<AnimationState>());
foreach(AnimationState state in states)
{
//do something with state
}
Answer by MightyJoe1 · Jan 12, 2015 at 10:19 PM
This code should work with C# to print all the AnimationClip names.
Just pass in a gameObject with an Animation Component.
public void PrintAnimations(GameObject character)
{
Animation anim = character.GetComponent<Animation>();
foreach(AnimationState state in anim)
{
Debug.Log(state.name);
}
}
Also you could play the animation by calling ...
...
anim.Play(state.name);
Answer by DaveA · May 07, 2011 at 07:55 PM
Seems like there should be. Using MonoDevelop and the auto-field (whatever that's called) looks like there's some undocumented things. Animation.GetClip("clipname") for instance. You might poke around there. How to get a list of clip names, I don't know. You might need to use Reflection
Yeah, I did notice GetClip is not documented. Hoping someone else had figured this out. Since it's publicly exposed in the editor, I was hoping it could be accessed via script somehow.
Your answer
Follow this Question
Related Questions
Trim animation to remove middle 1 Answer
Animation marked as legacy, still wont work 1 Answer
Animation won't play EVERY time it is called 1 Answer
Animation precision? 0 Answers
How to handle animations properly? 0 Answers