JMan Mousey JMan Mousey - 1 month ago 189
C# Question

Rename animation clips when importing in the Editor

I looked around the docs but didn't see any way to rename a clip as it is imported by a model importer. Is there an API call to name the clips? This is of particular importance because mecanim uses string names for the basis of its api calls and I want clips whose file names are right to have a specific naming convention even if the artist named it something else or will I have to rename thousands of files by hand?

https://s3.amazonaws.com/uploads.hipchat.com/20686/98498/FukbQZCxHofQopy/Untitled-2.jpg

Answer Source

Get ModelImporter from AssetImporter. You can then get the animation clips from ModelImporter.defaultClipAnimations. Loop through the animation clips, rename each one then save it.

Something like this:

using UnityEditor;
public class CreateAnimationClip : AssetPostprocessor
{
    void OnPreprocessAnimation()
    {
        ModelImporter modelImporter = assetImporter as ModelImporter;

        ModelImporterClipAnimation[] clipAnimations = modelImporter.defaultClipAnimations;

        //Modify/Rename animation clips?
        for (int i = 0; i < clipAnimations.Length; i++)
        {
            clipAnimations[i].name = "Your New Clip Name";
        }

        //Assign modiffied clip names back to modelImporter
        modelImporter.clipAnimations = clipAnimations;

        //Save
        modelImporter.SaveAndReimport();
    }
}