How in the world do I nest Animations manually?

This Animation controller has all it’s animation states nested under the controller. I think this is a great way to organize. Only issue is I can’t do this myself, the only way I’ve seen to get this setup is with “Auto Generate Animation” when using a UI>Button, but if I try to setup this type of relationship with other objects manually, I simply can’t nest the animations under the object.

Functionally it’s almost the same except other animation controllers can’t access another controller’s animations if they are nested, which is fine but I just want to know how to accomplish this. Is it possible to do this manually on our own?

TL;DR: UIButton’s Auto Generate Animation does something I can’t seem to recreate.

2 Likes

You can do it via API:

4 Likes

Perfect! Exactly what I needed to see.

It is working and I can add new AnimationClips to Controller just fine, but when I try to add this clip to newly created empty state it gives me an error :
Animation clip ‘End’ is not retargetable. Animation clips used within the Animator Controller need to have Muscle Definition set up in the Asset Importer Inspector.
Any tips?

I’m not sure. Is the clip an ‘imported’ clip. If it’s an asset on the HD you probably can’t add it as a child.

Possible feature request: could we have AnimationClips be nested in the AnimatorController by default? I don’t think I’ve ever once reused an AnimationClip in a different controller, and associating clips with the correct controller is…challenging at best.

2 Likes

It’s not “imported”, I’ve just added it like this:

AnimationClip clip = new AnimationClip();
clip.name = clipName;
AnimatorController controller = Selection.activeObject as AnimatorController;
AssetDatabase.AddObjectToAsset(clip, controller);
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(controller));

error appears when I try to assign such nested clip to motion field

BTW, Is there a way to remove nested clip?

2 Likes

Here is the method Unity uses to generate a transition

private static AnimationClip GenerateTriggerableTransition(string name, AnimatorController controller)
{
    AnimationClip animationClip = AnimatorController.AllocateAnimatorClip(name);
    AssetDatabase.AddObjectToAsset(animationClip, controller);
    State dst = AnimatorController.AddAnimationClipToController(controller, animationClip);
    controller.AddParameter(name, AnimatorControllerParameterType.Trigger);
    StateMachine stateMachine = controller.GetLayer(0).stateMachine;
    Transition transition = stateMachine.AddAnyStateTransition(dst);
    AnimatorCondition condition = transition.GetCondition(0);
    condition.mode = TransitionConditionMode.If;
    condition.parameter = name;
       
    return animationClip;
}

PS: If you are interested i’ve made a little class to help me create animator controllers with custom states just like the Auto Generator in Unity. Here’s the link: http://pastebin.com/ZLjZ1gda

3 Likes

Hey, thanks! This work just great!

1 Like