Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Get Animation Name (creating a generic animation pause script)

Discussion in 'Scripting' started by Aurecon_Unity, Apr 29, 2014.

  1. Aurecon_Unity

    Aurecon_Unity

    Joined:
    Jul 6, 2011
    Posts:
    241
    Hi All

    I am trying to create a generic script that pauses the animation on a particular object.

    For reasons that I won't go in to, I am using the legacy animation system, and meccanim is not an option.

    I can easily accomplish this with the:

    Code (csharp):
    1. animation["name here"].speed = 0;
    However, what if I don't know the name of the animation? This script will be applied to a number of objects with different animation names, so I want the script to first get the animation name applied to the object (I only ever have one animation component added, each with only one animation element).

    Any ideas?

    Cheers
     
  2. Keshav

    Keshav

    Joined:
    Jan 17, 2013
    Posts:
    11
    Code (csharp):
    1.  
    2. [RequireComponent(typeof(Animation))]
    3. public class AnimationClipPlayback : Monobehaviour{
    4.  
    5.          Animation animationComponent;
    6.          AnimationClip defaultClip;
    7.  
    8.          void Awake()
    9.          {
    10.                   animationComponent = GetComponent<Animation>();
    11.                   defaultClip = animationComponent.clip;
    12.          }
    13.  
    14.          public void Pause()
    15.          {
    16.                   animationComponent[defaultClip.name].speed = 0f;
    17.          }
    18.  
    19. }
    20.  
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    This will only pause the default clip and therefore doesn't meet the requirements that OP put forth.

    There's no way to get the currently playing clip because there are many instances where there is either no clip playing or several clips are blended together. The Animation component has an IsPlaying method that takes a string clip name argument. You could enumerate all the clips in the animation and pause them if IsPlaying(clipName) is true.

    Alternately - wrap any calls to animation[].Play in your own method that stores the last played animation.
     
    Last edited: Apr 29, 2014
  4. Aurecon_Unity

    Aurecon_Unity

    Joined:
    Jul 6, 2011
    Posts:
    241
    Thanks to both of you for your help.

    I might not have been clear - each object will only ever have one animation component assigned, with only one clip contained therein. So it appears that Keshav's answer works in my case - I did some tests and I can't detect any funky behavior.

    If I ever need to expand on this, I'll definitely be using the advice given by KelsoMRK.

    So thanks again!
     
  5. Keshav

    Keshav

    Joined:
    Jan 17, 2013
    Posts:
    11
    Thanks mickyg :) Glad to be of help..

    Instead of having multiple Objects having one Animation Component and one Animation Clip. You could have One Animation Component on one Object and have multiple Scripts each having a reference to a AnimationClip and The Main Animation Component. On Awake you could add the AnimationClips to Animation Component. This will let you manage individual AnimationClips with just one Animation Component.