Search Unity

Animation.AddClip and Playing the Clip

Discussion in 'Animation' started by ThatGuy2, Oct 9, 2014.

  1. ThatGuy2

    ThatGuy2

    Joined:
    Oct 9, 2014
    Posts:
    12
    In the following, I'm attempting to create a Clip and play it. I'm successfully instantiating the animation object (anim). I verified that "openClip" is not null. However, when I execute "anim.Play("openScreen")" for the clip, the clip does not play. I verified the return value, which is TRUE. That is, Unity thinks it played the clip. The model has a single baked animation "Scene", which is from frames 1 through 89. Scene will eventually have other clips.

    1. privateAnimation anim;
    2. privateAnimationClip openClip;

    3. publicoverridevoidOnStart(PartModule.StartState state)
    4. {
    5. anim = part.FindModelAnimators("Scene")[0];
    6. openClip = newAnimationClip();
    7. anim.AddClip(openClip,"openScreen",1,89,false);
    8. anim.Play("openScreen");
    9. }
    I could create the clips in the Unity Editor. However, I want to attach AnimationEvents to the clips. Specifically, I want to know when each clip has finished playing.

    So:

    1) what is incorrect in my method of creating and playing clips in the script?

    2) can I attach an AnimationEvent, in a script, to a clip I define in the Unity Editor versus doing it in the compiled script?


    Note: I'm not using the Editor Scripting Tool; I'm compiling in C# as a DLL

    I displayed the clip length (openClip.length); it showed a length of 1. However, the clip is really 3 seconds long. Also, "anim.Play();" works fine as well; it plays all 90 frames though. I'm using the Legacy animation system, because Kerbal Space Program (which I need this for) requires that.
     
    Last edited: Oct 10, 2014
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    newly created clip for 4.x are set to be use with mecanim. You will need to change the animation Type from mecanim to legacy. The only way to change this attribute is with serialized property like this:
    Code (CSharp):
    1.    
    2.     SerializedObject obj = new SerializedObject(openClip);
    3.     SerializedProperty animationType = obj.FindProperty("m_AnimationType");
    4.  
    5.     animationType.IntValue = 1;
    6.     obj.ApplyModifiedProperties();
    7.  
     
  3. ThatGuy2

    ThatGuy2

    Joined:
    Oct 9, 2014
    Posts:
    12
    Mecanim_Dev, thank you for answering.

    When I imported the animation via FBX from Blender, I set the animation type to Legacy in the Inspector.

    So, do I still need to use your procedure? If so, I'll jump right on that. Thanks for the specific code. Secondly, what would I use AddClip() on then?
     
  4. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    No, for imported clip, the model importer will automatic set the animation clip type to legacy if the rig type is set to Legacy. You only need to do that for clip created from code.

    Again clip imported are automaticaly added to the animation component for you. But for clip created from script you need to manually add the clip to your animation component like you did.
     
  5. ThatGuy2

    ThatGuy2

    Joined:
    Oct 9, 2014
    Posts:
    12
    However, the clip is not being created. "openScreen.Length" returns a value of 1. The 89-frame animation is 3.5 seconds.

    What should my AddClip() code look like to create and play a clip; lets say from frame 1 to 50?
     
  6. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Use the Animation.this[string] API to give you the AnimationClip that you imported, and which has 89 animation frames. This is then your openClip. When you use AddClip() you can create a copy of this animation with the frame range you want. As I have said in your other post, when you create a new AnimationClip by using the constructor, you get a new, empty clip. If you want to edit a clip you've imported, pass that into the AddClip() API as the documentation shows.

    Maybe Animation is a bit confusing as an object type? Animation is a container for animation clips. Clips that you import get added into an array of clips that the Animation holds. You see these in the inspector. The screen shot shows a Animation where I've added a single clip which I created in the Animation Window. You can obviously import clips too.

    So, this Animation has a single clip, called clip. Animation also has a concept of a default clip, which the scripting API returns to you as .clip. AddClip() will create a new animation clip, and add it to the array of clips. You can take existing clips, and shorten them.

    (Hmm. Not been able to reliably get a clip I've created in the Animation window onto the Animation component. Something odd here.)
     
  7. ThatGuy2

    ThatGuy2

    Joined:
    Oct 9, 2014
    Posts:
    12
    Graham, thanks again. See my note in the other thread. I believe I misunderstood what AddClip() is supposed to do.