Creating an animation with attached animation clips

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().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?

2168565--143471--animations.png

Thank you for your help.

The key is: Unity - Scripting API: AssetDatabase.AddObjectToAsset

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.

    [MenuItem("Anim/Add Clip")]
    static void AddClip()
    {
        // Create animator
        Animator anim = new Animator();
        //anim.name = "MyAnim";

        AnimationClip clip = new AnimationClip();
        clip.name = "Left";

        AssetDatabase.AddObjectToAsset(clip, anim);
    }

Suggestions?

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.

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:

    [MenuItem("Anim/Add Clip")]
    static void AddClip()
    {
        Animator anim = new Animator();
        AssetDatabase.CreateAsset(anim, "Assets/Sphere.controller");    // ERROR

        AnimationClip clip = new AnimationClip();
        clip.name = "Up";

        AssetDatabase.AddObjectToAsset(clip, anim);
    }

Any other suggestions?

Oh, right, that’s not how you create an Animator asset. Use CreateAnimatorControllerAtPath. You might want to watch this video on the topic.

You are almost there, just remember that the Animator is a runtime component not the asset.

The saved asset is the AnimatorController which is where you add clip, like @superpig is suggesting use

and then add your clip asset to the same controller file with

Thank you Mecanim.Dev. That worked.