Search Unity

Embedding Motion Clips in the Controller Asset.

Discussion in 'Animation' started by xCyborg, Jan 15, 2016.

  1. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    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?
     
    LilGames likes this.
  2. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
  3. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
  4. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
  5. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    I use Selection. So a very simple version is :
    Code (CSharp):
    1. //  ==================================================================================================================
    2. //    <description>NestAnimClips.cs - Nesting AnimationClips inside an AnimationContoller.</description>
    3. //  <author>ZombieGorilla for Unity Forums</author>
    4. //     <version>1.0</version>
    5. //    <date>2016-02-14</date>
    6. //  ==================================================================================================================
    7.  
    8. using UnityEngine;
    9. using System.Collections;
    10. using UnityEditor;
    11. using System.Collections.Generic;
    12.  
    13. public class NestAnimClips : MonoBehaviour
    14. {
    15.     [MenuItem("Assets/Nest AnimClips in Controller")]
    16.     static public void nestAnimClips()
    17.     {
    18.         UnityEditor.Animations.AnimatorController anim_controller = null;
    19.         List<AnimationClip> clips = new List<AnimationClip>();
    20.         var objs = Selection.objects;
    21.        
    22.         for (int i = 0; i < objs.Length; i++)
    23.         {
    24.             if(objs[i].GetType() == typeof(UnityEditor.Animations.AnimatorController) )
    25.             {  
    26.                 if(anim_controller != null)
    27.                 {
    28.                     Debug.Log("<color=red>Please Select only ONE controller.</color>");
    29.                     return;
    30.                 }
    31.                 else
    32.                 {
    33.                     anim_controller = (UnityEditor.Animations.AnimatorController)objs[i];
    34.                 }
    35.             }
    36.            
    37.             if(objs[i].GetType() == typeof(AnimationClip) ) { clips.Add((AnimationClip)objs[i]); }
    38.         }
    39.        
    40.         if(anim_controller!=null && clips.Count>0)
    41.         {
    42.             foreach (AnimationClip ac in clips)
    43.             {
    44.                 var new_ac = Object.Instantiate(ac) as AnimationClip;
    45.                 new_ac.name = ac.name;
    46.                
    47.                 AssetDatabase.AddObjectToAsset(new_ac, anim_controller);
    48.                 AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(new_ac));
    49.                 AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(ac));
    50.             }
    51.             Debug.Log("<color=orange>Added "+clips.Count.ToString()+" clips to controller: </color><color=yellow>" + anim_controller.name + "</color>");
    52.         }
    53.         else
    54.         {
    55.             Debug.Log("<color=red>Nothing done. Select a controller and anim clips to nest.</color>");
    56.         }
    57.     }
    58. }
    59.  
    60.  
    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.
     
    mightee-cactus likes this.
  6. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    Last edited: Feb 14, 2016
  7. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
  8. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    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:)
    Code (csharp):
    1.  
    2. //  ==================================================================================================================
    3. //    <description>NestAnimClips.cs - Nesting AnimationClips inside an AnimationContoller.</description>
    4. //  <author>ZombieGorilla for Unity Forums</author>
    5. //     <version>1.0</version>
    6. //    <date>2016-02-14</date>
    7. //  ==================================================================================================================
    8.  
    9. using UnityEngine;
    10. using System.Collections;
    11. using UnityEditor;
    12. using System.Collections.Generic;
    13.  
    14. public class NestAnimClips : MonoBehaviour
    15. {
    16.     [MenuItem("Assets/Nest AnimClips in Controller")]
    17.     static public void nestAnimClips()
    18.     {
    19.         UnityEditor.Animations.AnimatorController anim_controller = null;
    20.         AnimationClip[] clips = null;
    21.  
    22.         if(Selection.activeObject.GetType() == typeof(UnityEditor.Animations.AnimatorController) )
    23.         {
    24.             anim_controller = (UnityEditor.Animations.AnimatorController)Selection.activeObject;
    25.             clips = anim_controller.animationClips;
    26.  
    27.             if(anim_controller!=null && clips.Length>0)
    28.             {
    29.                 foreach (AnimationClip ac in clips)
    30.                 {
    31.                     var new_ac = Object.Instantiate(ac) as AnimationClip;
    32.                     new_ac.name = ac.name;
    33.                
    34.                     AssetDatabase.AddObjectToAsset(new_ac, anim_controller);
    35.                     AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(new_ac));
    36.                     AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(ac));
    37.                 }
    38.                 Debug.Log("<color=orange>Added "+clips.Length.ToString()+" clips to controller: </color><color=yellow>" + anim_controller.name + "</color>");
    39.             }
    40.             else
    41.             {
    42.                 Debug.Log("<color=red>Nothing done. Select a controller that has anim clips to nest.</color>");
    43.             }
    44.         }
    45.  
    46.     }
    47. }
    48.  
     
    zombiegorilla likes this.
  9. bourriquet

    bourriquet

    Joined:
    Jul 17, 2012
    Posts:
    181
    This works for Controllers but not for Override Controllers.
    If you want it to work for both, replace
    Code (CSharp):
    1. UnityEditor.Animations.AnimatorController
    with
    Code (CSharp):
    1. RuntimeAnimatorController
    and
    Code (CSharp):
    1. something.GetType() == typeof(UnityEditor.Animations.AnimatorController)
    with
    Code (CSharp):
    1. typeof(RuntimeAnimatorController).IsAssignableFrom(something.GetType())
     
    xCyborg likes this.
  10. xzaxzaazx

    xzaxzaazx

    Joined:
    Mar 18, 2019
    Posts:
    10