Search Unity

Animation clips in array !!?

Discussion in 'Scripting' started by crazyosachou, Oct 19, 2010.

  1. crazyosachou

    crazyosachou

    Joined:
    Sep 27, 2010
    Posts:
    176
    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
     
  2. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    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.

    Code (csharp):
    1.  
    2. public class AnimationManager : MonoBehaviour
    3. {
    4.     Animation anim;
    5.    
    6.     void Awake()
    7.     {
    8.         anim = animation;  
    9.     }
    10.    
    11.     public AnimationClip this [int index]
    12.     {
    13.         get
    14.         {
    15.             int i = 0;
    16.             foreach(AnimationState clip in anim)
    17.             {
    18.                 if(i = index)
    19.                 {
    20.                     return clip;   
    21.                 }
    22.                 i++;   
    23.             }
    24. return null;
    25.         }
    26.     }
    27. }
    28.  
     
  3. crazyosachou

    crazyosachou

    Joined:
    Sep 27, 2010
    Posts:
    176
    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
     
  4. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    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.
     
  5. crazyosachou

    crazyosachou

    Joined:
    Sep 27, 2010
    Posts:
    176
    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)
     
  6. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    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.
     
  7. Charles-Van-Norman

    Charles-Van-Norman

    Joined:
    Aug 10, 2010
    Posts:
    86
  8. PhM340

    PhM340

    Joined:
    Dec 23, 2010
    Posts:
    8
  9. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Another approach is to have an array with the animation names in it and refer to them using that:-
    Code (csharp):
    1. var animNames: String[];  // Set this in the inspector with the animation names in order.
    2.   ...
    3. animation.Play(animNames[2]);  // ...or whatever.
     
  10. Winterblood

    Winterblood

    Joined:
    Aug 23, 2011
    Posts:
    9
    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...
     
  11. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    erm...

    Code (csharp):
    1.  
    2. function OnGUI()
    3. {
    4.   y = 0;
    5.   for (var ast : AnimationState in Avatar.animation)
    6.   {
    7.      GUI.Label(Rect(400,((y++)*30)+10,300,25), "Animation "+y+ " = " + ast.name);
    8.   }
    9. }
    10.  
    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...???