- Home /
How to move Animation Clips into Animator Controller?
How can I move my own animation clips into an animation controller so it is a child in the project hierarchy. See pic below. I want them to be children of the controller so it is easier to maintain the project structure and know which clip belongs to which controller e.g. here all the new clips belong to More Button Controller
.
I've tried what is suggested here. It doesn't work with existing clips. It throws an error saying 'Clip already exists'.
I also tried removing and adding it and it does show up correctly in hierarchy but it also keeps the old copy. So now there are two items in the project window.
AssetDatabase.RemoveObjectFromAsset(animationClip);
AssetDatabase.AddObjectToAsset(animationClip, animatorController);
I've also tried AssetDatabase.MoveAsset
where new location is AnimationController/ClipName
. That doesn't work either; it says 'Cannot move.'
Is there no way to move existing clips to animator controller?
Answer by lordlycastle · Jul 03, 2019 at 04:51 PM
I was able to finally do this by creating a copy and then moving that into the object. It however, does not update it's references. So you'll have to manually replace the old ones. So it is best to do this in start.
NOTE: once you add it to asset I was not able to find a way to remove it! So create backup/VC.
/// <summary>
/// Moves existing animation clips to be part of animator controller.
/// It does this by creating a copy and then adding a copy. It has option to delete the old copy.
/// </summary>
/// <param name="animationClip"></param>
private void CopyExistingClip(AnimationClip animationClip)
{
var newCopy = Object.Instantiate(animationClip) as AnimationClip;
newCopy.name = animationClip.name;
AddClipToAnimatorController(newCopy);
if (deleteOldCopies)
{
AssetDatabase.MoveAssetToTrash(AssetDatabase.GetAssetPath(animationClip));
}
}
/// <summary>
/// Creates a new clip and adds it to the animator controller.
/// </summary>
/// <param name="clipName"></param>
private void CreateNewAnimationClip(string clipName)
{
var newClip = new AnimationClip {name = clipName};
AddClipToAnimatorController(newClip);
}
/// <summary>
/// Add the clip to animator controller and re-imports
/// </summary>
/// <param name="animationClip"></param>
private void AddClipToAnimatorController(AnimationClip animationClip)
{
AssetDatabase.AddObjectToAsset(animationClip, animatorController);
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(animationClip));
}
Answer by danielandrino · Jul 22, 2020 at 02:20 PM
There is a easier way of moving an animation clip to a new controller.
Create the new Controller
In the Animator window, add a new empty state
Select the created state and update the motion parameter (in the inspector window) with the old animation clip
You will need to manually update the animation clip references. In the Hierarchy window, select the object containing the new controller. In the Animation window, select the clip and then click twice (not double click) in each of the missing references text. Update the text to the new hierarchy structure.
Hope it helps!