Embedding Motion Clips in the Controller Asset.

Hello, in the UI Button component for example, you can create an animation using “Auto Generate Animation”, it creates a beautiful Controller asset containing all the state clips in the project window.
But when I create my own controller and states they’re each seperate.

Can you have it look like the UI animation?

1 Like

Bump

Can be a cool fix, but how would I know which clips go with which controllers?

I use Selection. So a very simple version is :

//  ==================================================================================================================
//    <description>NestAnimClips.cs - Nesting AnimationClips inside an AnimationContoller.</description>
//  <author>ZombieGorilla for Unity Forums</author>
//     <version>1.0</version>
//    <date>2016-02-14</date>
//  ==================================================================================================================

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;

public class NestAnimClips : MonoBehaviour
{
    [MenuItem("Assets/Nest AnimClips in Controller")]
    static public void nestAnimClips()
    {
        UnityEditor.Animations.AnimatorController anim_controller = null;
        List<AnimationClip> clips = new List<AnimationClip>();
        var objs = Selection.objects;
       
        for (int i = 0; i < objs.Length; i++)
        {
            if(objs[i].GetType() == typeof(UnityEditor.Animations.AnimatorController) )
            {   
                if(anim_controller != null)
                {
                    Debug.Log("<color=red>Please Select only ONE controller.</color>");
                    return;
                }
                else
                {
                    anim_controller = (UnityEditor.Animations.AnimatorController)objs[i];
                }
            }
           
            if(objs[i].GetType() == typeof(AnimationClip) ) { clips.Add((AnimationClip)objs[i]); }
        }
       
        if(anim_controller!=null && clips.Count>0)
        {
            foreach (AnimationClip ac in clips)
            {
                var new_ac = Object.Instantiate(ac) as AnimationClip;
                new_ac.name = ac.name;
               
                AssetDatabase.AddObjectToAsset(new_ac, anim_controller);
                AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(new_ac));
                AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(ac));
            }
            Debug.Log("<color=orange>Added "+clips.Count.ToString()+" clips to controller: </color><color=yellow>" + anim_controller.name + "</color>");
        }
        else
        {
            Debug.Log("<color=red>Nothing done. Select a controller and anim clips to nest.</color>");
        }
    }
}

So this quick script (dropped in an Editor folder), will add a context menu item when you right click on an asset in the project folder.

To use select an Animator and as many Animation Clips as you want, and right+click and select it in the menu. It will duplicate the clips in add them to the Animator, the destroys the original.

WARNING: since you can’t “move” the clips into an asset, they have to be duplicated because the original exists already in the assetdatabase. This means that if you have refs to the clips (like in the AnimatorController), you will need to relink them. It works best if you do before you start animating, but it does work with existing animations if you just relink.

This is just a quick and dirty version. The one I built for work, goes the extra step of relinking after duplication, and also allows for removal. But the above script is a good starting place.

1 Like

Thanks for your effort, it’s a starting point, I’ll try to iron it out until Unity maybe considers this.
Too bad UnityEditor.Animations.AnimatorController.animationClips doesn’t seem to work:(

It works just fine:
2525600--175208--Screen Shot 2016-02-24 at 1.06.24 PM.png

Oh yes it works, just not with clips already nested in (which is kinda helpful actually), check this out:
You only have to select the Controller, and if any of its clips are not already nested they get added to it:)

//  ==================================================================================================================
//    <description>NestAnimClips.cs - Nesting AnimationClips inside an AnimationContoller.</description>
//  <author>ZombieGorilla for Unity Forums</author>
//     <version>1.0</version>
//    <date>2016-02-14</date>
//  ==================================================================================================================

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;

public class NestAnimClips : MonoBehaviour
{
    [MenuItem("Assets/Nest AnimClips in Controller")]
    static public void nestAnimClips()
    {
        UnityEditor.Animations.AnimatorController anim_controller = null;
        AnimationClip[] clips = null;

        if(Selection.activeObject.GetType() == typeof(UnityEditor.Animations.AnimatorController) )
        {
            anim_controller = (UnityEditor.Animations.AnimatorController)Selection.activeObject;
            clips = anim_controller.animationClips;

            if(anim_controller!=null && clips.Length>0)
            {
                foreach (AnimationClip ac in clips)
                {
                    var new_ac = Object.Instantiate(ac) as AnimationClip;
                    new_ac.name = ac.name;
                
                    AssetDatabase.AddObjectToAsset(new_ac, anim_controller);
                    AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(new_ac));
                    AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(ac));
                }
                Debug.Log("<color=orange>Added "+clips.Length.ToString()+" clips to controller: </color><color=yellow>" + anim_controller.name + "</color>");
            }
            else
            {
                Debug.Log("<color=red>Nothing done. Select a controller that has anim clips to nest.</color>");
            }
        }

    }
}
1 Like

This works for Controllers but not for Override Controllers.
If you want it to work for both, replace

UnityEditor.Animations.AnimatorController

with

RuntimeAnimatorController

and

something.GetType() == typeof(UnityEditor.Animations.AnimatorController)

with

typeof(RuntimeAnimatorController).IsAssignableFrom(something.GetType())
1 Like

Look at my script answer to this: How to add .anim files into a controller?