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?
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?
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
}
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.
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;
}
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
}
}
}
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
Here is the way to index AnimationClips or AnimationStates as they are called:
GameObject goPlayer = ... some object with animation
int animCount = goPlayer.animation.GetClipCount(); // clip?! Unity people, why not state??
Debug.Log("Animations found: " + animCount );
int idx=0;
foreach (AnimationState anim in goPlayer.animation)
{
// show me my animation name!!!
Debug.Log("Animation ("+ idx +"): " + anim.name);
idx ++;
}
THIS WORKS
arms is just the GameObject with the animation on it and 0 the index. Arms.GetComponent<Animation>()[0]