How To Play Clips From Animation Index Unity
- Spaces
- Default
- Help Room
- META
- Moderators
-
- Topics
- Questions
- Users
- Badges
- Home /
two
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 instance the second one)?
I know the scripting reference explains how to select it by prune name, but is there a mode to play information technology by index number?
7 Replies
· Add together your respond
- Sort:
3
Reply by Kerihobo · Jul 27, 2015 at 10:08 PM
I know this question is old, only I thought I'd share my gear up.
I had fourscore blithe characters all with the same 5 animations, though some of them were named differently because information technology would be the walk cycle for a crawling infant 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'due south it, that's all my script had lol. Then I applied it to the prefabs of all my fourscore chars, and listed their animations in respective order:
bind walk blanch trip the light fantastic suicide
And so in script I merely said
gameObject.animation.Play(getComponent(AnimList).animList[0]);
basically I couldn't access THEIR array and then I made my own array that I COULD access.
1
Reply past Jaap Kreijkamp · Nov 24, 2009 at 11:19 PM
I believe yous tin can't, stupid, isn't information technology? You CAN however iterate over the animationclips with the (in C#) foreach keyword:
foreach (AnimationClip clip in animation) { // practise initialisation or something on clip }
Sorry, but this doesnt work?
InvalidCastException: Cannot cast from source type to destination type.
Information technology's AnimationState
, not AnimationClip
- BerggreenD$$anonymous$$, my answer on your question will exist visible when approved past a moderator.
you need to apply AnimationState not AnimationClip
Thanks, didnt see it before posting my ain. I've upvoted your comment.
Doesn't this implicitly mean that all legacy operations requiring this blazon of iteration are doomed to create garbage?
1
Answer by shopguy · February 23, 2015 at 05:10 AM
Kind of simple, simply if it saves someone some time...
string ClipIndexToName(int alphabetize) { AnimationClip clip = GetClipByIndex(index); if (clip == null) return null; return clip.name; } AnimationClip GetClipByIndex(int index) { int i = 0; foreach (AnimationState animationState in blitheness) { if (i == alphabetize) render animationState.clip; i++; } return null; }
Update, since the above seems to have broke with Unity v (or maybe was always frail and now I'm just finding out), I concluded upwards changing GetClipByIndex to this. Also bad I have to difficult-lawmaking the clip names, a petty extra piece of work:
AnimationClip GetClipByIndex(int index) { string[] ClipNames = { "StepForward", "StepBackward", "ShortThrow", "LongThrow", "BackwardThrow", "SoftThrow", "Balderdash", "Hurt" }; Animation blitheness = GetComponent<Blitheness>(); render animation[ClipNames[index]].clip; }
FYI - This seems to have cleaved equally of Unity 5... or at least, the clips aren't ever in the aforementioned society, 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 guild.
1
Answer by splitter20 · Mar 28, 2017 at 03:02 AM
Not sure if this is how it'due south supposed to piece of work, but this does exactly what your asking.
using System.Collections; using System.Collections.Generic; using UnityEngine; public form EnemyMovement : MonoBehaviour { Animation anim; public List<cord> animNames; void Start(){ anim = GetComponent<Blitheness> (); 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 retrieve you lot forgot to initialize empty listing like this: private Listing<string> animNames = new List<cord>();
- ane
- 2
- ›
Your answer
Follow this Question
Related Questions
Source: https://answers.unity.com/questions/8245/how-to-select-an-animation-clip-by-index-number.html
Posted by: blunthaideatel.blogspot.com
And you cannot then edit the animations easily into the Animation Window. This is, it only does non like custom component with AnimationClip references.