Animation clips in array !!?

Hi,

How can i access to animation clips using there order index instead of there names ?!

something like : animation.clips[0] , animation.clips[1] … etc , is it possible ?

thanks

It’s a bit of a slow workaround for sealed classes, but that should allow you to access indexed animations via AnimationManager using the syntax animationManager[5] or any other index you prefer. You’ll have to test for preserved order, and make sure the component has both an Animation and AnimationManager component or attach it into some sort of Utilities.

public class AnimationManager : MonoBehaviour
{
	Animation anim;
	
	void Awake()
	{
		anim = animation;	
	}
	
	public AnimationClip this [int index]
	{
      	get
      	{
			int i = 0;
			foreach(AnimationState clip in anim)
			{
				if(i = index)
				{
					return clip;	
				}
				i++;	
			}
return null;
      	}
	} 
}

thanks, i’ll try it and see if it works , but really no sens that there’s no simple ways to access to that :s

It’s very possible that it is the same reason you can’t access an index for Transform children.

Why do you want to access an Animation by index? If you want to access all animations, use Foreach. If you want to access a specific animation, you should really have it’s name, or else how do you know it is the right animation? Using indices and not names wild, unreadable lists of integers when there is a perfectly good name for each animation that would make it 10x more readable.

well, imagine that ur animating some objects in unity3d (using animation window) , u’ll notice that u must name each clip of animation u create and u must save this clip into assets folder … so since u have a lot of things to animate, u’ll forsure have many files .anim in the assets folder …

the deal is , i love using prefabs , so the script in prefabs must be global that u can apply to anyobject …

i create a prefabs that does this : (if (conditions1) then (play(clip1)) else (play(clip 2)) … etc ( i really create some complicated prefabs)

so if i need to know all the names of my objects animation in my scene , i wouldn’t name that “PREFABS” and its really boring , so instead of that , i want to use a script that will do : ( IF (codition1)then( play(the_1st_animation watever its name is ) else (play(the_2st_animation watever its name is) … etc

conclusion : i need to say (What ever my object is , and wat ever my objects animations name is , play the 1st or play the 2nd clip ,or the 3rd clip … etc)

The animations should be named for what they do. If you need to access them by index, then index them -

Action1, Action2, Action3, et cetera should be perfectly sufficient.

You might be able to achieve your goal using the foreach command in c#:
http://answers.unity3d.com/questions/732/how-to-select-an-animation-clip-by-index-number

Hi,

I have the same problem since 3 days without a solution : http://forum.unity3d.com/threads/74075-Animation-(*.anim), including on this forum is the best source available in the world …

Who will have a solution for handling a list of animations?

Another approach is to have an array with the animation names in it and refer to them using that:-

var animNames: String[];  // Set this in the inspector with the animation names in order.
  ...
animation.Play(animNames[2]);  // ...or whatever.

Anyone know if the anim names are hashed internally? I just want to be sure it’s not doing a series of string compares every time I request an anim, because that would be slowww…

erm…

function OnGUI()
{
  y = 0;
  for (var ast : AnimationState in Avatar.animation)
  {
     GUI.Label(Rect(400,((y++)*30)+10,300,25), "Animation "+y+ " = " + ast.name);
  }
}

Not sure if that is gonna help you but at least you can get the animation’s name via index so then you can still call the animation by name after getting the animation name via index…???