Adding animations using AssetPostprocessor

I'm trying to add animation clips to a newly imported model by using an AssetPostprocessor editor scripts. Here's what I have:

public class AddAnimations : AssetPostprocessor
    private void OnPostprocessModel(GameObject gameObject)
        gameObject.animation.AddClip(new AnimationClip(), "MyNewAnimation");

This "works", in that it executes when I import my model, and even adds an extra clip slot, but doesn't assign the "MyNewAnimation" clip to that slot (see below).

alt text

Notice that it has created the new slot at Element 1, but hasn't assigned it. If I select the drop down menu next to it, MyNewAnimation appears in the list, so I can assign it manually, but that defeats the purpose.

[Edit] Also note that I have tried replacing the "new AnimationClip()" with a dummy clip added the traditional way through the Asset Importer. This made no difference to the outcome.

Ok, I've managed to figure it out. You need to pre-process it and use ModelImporter.


ModelImporter importer = assetImporter as ModelImporter;

ModelImporterClipAnimation clip = new ModelImporterClipAnimation();
clip.firstFrame = 0;
clip.lastFrame = 1000;
clip.loop = true; = "Test";

ModelImporterClipAnimation[] clips = new ModelImporterClipAnimation[]

importer.clipAnimations = clips;

Of course, you can add as many clips as you want.

One of the problems here is that the new AnimationClip() you make, is not persistent. You could try making it into an asset using CreateAsset(), (but checking if it's not there yet already, and if it is, using LoadAssetAtPath()).

I usually choose not to bother with doing this whole dance, and just don't assign any animations on import done, but rather just attach a monobehaviour that references the animationclips I want to use, and then doing AddClip() at runtime.

In editor scripts (in Unity 2.6 forward) you can use AnimationUtility.GetAnimationClips() and AnimationUtility.SetAnimationClips() to manipulate the array of animations in the Animation component. Animation.AddClip() is not really meant for that, as it only adds an AnimationState, not an AnimationClip.