- Home /
How to select an animation clip by index number?
I have an created an animation in the editor with multiple clips,
How can I play a specific clip (for example the second one)?
I know the scripting reference explains how to select it by clip name, but is there a way to play it by index number?
Answer by Kerihobo · Jul 27, 2015 at 10:08 PM
I know this question is old, but I thought I'd share my fix.
I had 80 animated characters all with the same 5 animations, though some of them were named differently because it would be the walk cycle for a crawling baby or an old man with a walking stick, therefore different from the walk cycle of my generic humans.
What I did was create a script that just had a variable for animationClips.
var animList : AnimationClip[];
That's it, that's all my script had lol. Then I applied it to the prefabs of all my 80 chars, and listed their animations in respective order:
bind walk flinch dance suicide
Then in script I just said
gameObject.animation.Play(getComponent(AnimList).animList[0]);
basically I couldn't access THEIR array so I made my own array that I COULD access.
And you cannot then edit the animations easily into the Animation Window. This is, it just does not like custom component with AnimationClip references.
Answer by Jaap Kreijkamp · Nov 24, 2009 at 11:19 PM
I believe you can't, stupid, isn't it? You CAN however iterate over the animationclips with the (in C#) foreach keyword:
foreach (AnimationClip clip in animation) {
// do initialisation or something on clip
}
Sorry, but this doesnt work?
InvalidCastException: Cannot cast from source type to destination type.
It's AnimationState
, not AnimationClip
- BerggreenD$$anonymous$$, my answer on your question will be visible when approved by a moderator.
Thanks, didnt see it before posting my own. I've upvoted your comment.
Doesn't this implicitly mean that all legacy operations requiring this type of iteration are doomed to create garbage?
Answer by shopguy · Feb 23, 2015 at 05:10 AM
Kind of simple, but if it saves someone some time...
string ClipIndexToName(int index)
{
AnimationClip clip = GetClipByIndex(index);
if (clip == null)
return null;
return clip.name;
}
AnimationClip GetClipByIndex(int index)
{
int i = 0;
foreach (AnimationState animationState in animation)
{
if (i == index)
return animationState.clip;
i++;
}
return null;
}
Update, since the above seems to have broke with Unity 5 (or maybe was always fragile and now I'm just finding out), I ended up changing GetClipByIndex to this. Too bad I have to hard-code the clip names, a little extra work:
AnimationClip GetClipByIndex(int index)
{
string[] ClipNames = { "StepForward", "StepBackward", "ShortThrow", "LongThrow", "BackwardThrow", "SoftThrow", "Bull", "Hurt" };
Animation animation = GetComponent<Animation>();
return animation[ClipNames[index]].clip;
}
FYI - This seems to have broken as of Unity 5... or at least, the clips aren't always in the same order, the order you would expect (what they are in the editor), which kind of defeats the purpose of using an index to begin with, if you can't be sure of the order.
Answer by splitter20 · Mar 28, 2017 at 03:02 AM
Not sure if this is how it's supposed to work, but this does exactly what your asking.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMovement : MonoBehaviour {
Animation anim;
public List<string> animNames;
void Start(){
anim = GetComponent<Animation> ();
foreach (AnimationState clip in anim) {
animNames.Add(clip.name.ToString());
}
}
void Update(){
if (//something happens//) {
anim.CrossFade (animNames [1]); //plays animation in element1 spot
} else {
anim.CrossFade (animNames [0]); //plays animation in element0 spot
}
}
}
Thanks this works, but I think you forgot to initialize empty list like this: private List<string> animNames = new List<string>();
Answer by Charles-Van-Norman · Jan 18, 2011 at 05:43 AM
Animations in Array: Someone has tried to make a unique class to attach to the gameobject you're animating. I can't get it to work, but I understand the idea ..... I think. Anyway here is the thread: http://forum.unity3d.com/threads/64262-Animation-clips-in-array-!!?highlight=animation+clip+index