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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Creating an animation with attached animation clips

Discussion in 'Animation' started by TxAg03, Jun 20, 2015.

  1. TxAg03

    TxAg03

    Joined:
    May 20, 2015
    Posts:
    21
    Am currently animating some of my gameobjects by creating an animation controller and animation clips specific for that gameobject. All of the animations clips contain a start animation. Currently I have to create separate animation clips, like gameobject_Start and gameobject_Stop. I would like to reuse the same script and just call gameobject.GetComponent<Animation>().play("Start") instead of passing in manually gameobject_Start.

    The example I'm trying to duplicate is the UI Button animation transitions. When you change the Transition type to animation and auto generate, it creates an animation controller and sub animation clips. How can I "marry" the Start and Stop animations to a parent animation controller?

    animations.png

    Thank you for your help.
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,614
  3. TxAg03

    TxAg03

    Joined:
    May 20, 2015
    Posts:
    21
    Thank you superpig for the link. I was not able to figure out how to get it to add an animationclip to an animator that I had already set up in my project. I tried creating a new animator and animationclip like in the example, but always got a null reference exception when trying to add the clip to the animator. Below is the code I can't get to work. I'm assuming it's because I have not specified any values for the Animator/AnimationClip.

    Code (CSharp):
    1.     [MenuItem("Anim/Add Clip")]
    2.     static void AddClip()
    3.     {
    4.         // Create animator
    5.         Animator anim = new Animator();
    6.         //anim.name = "MyAnim";
    7.  
    8.         AnimationClip clip = new AnimationClip();
    9.         clip.name = "Left";
    10.  
    11.         AssetDatabase.AddObjectToAsset(clip, anim);
    12.     }
    Suggestions?
     
  4. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,614
    You need to associate the Animator with an asset before you can then add other objects to it - use AssetDatabase.CreateAsset with your 'anim' first, then AddObjectToAsset with 'clip' after that.
     
  5. TxAg03

    TxAg03

    Joined:
    May 20, 2015
    Posts:
    21
    I tried to create the asset first and still got a null reference exception. I couldn't find a constructor listed for the Animator, so I have no clue what arguments I can/need to pass to it.

    Here's my new attempt:

    Code (CSharp):
    1.     [MenuItem("Anim/Add Clip")]
    2.     static void AddClip()
    3.     {
    4.         Animator anim = new Animator();
    5.         AssetDatabase.CreateAsset(anim, "Assets/Sphere.controller");    // ERROR
    6.  
    7.         AnimationClip clip = new AnimationClip();
    8.         clip.name = "Up";
    9.  
    10.         AssetDatabase.AddObjectToAsset(clip, anim);
    11.     }
    Any other suggestions?
     
  6. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,614
  7. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
  8. TxAg03

    TxAg03

    Joined:
    May 20, 2015
    Posts:
    21
    Thank you Mecanim.Dev. That worked.