- Home /
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).
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.
Note: Obviously that isn't my entire script. It also has things to make sure it's the right asset it's processing and it adds many more clips -- this is just a $$anonymous$$imal script to reproduce the problem.
Answer by Peter Alexander · Dec 21, 2009 at 10:12 AM
Ok, I've managed to figure it out. You need to pre-process it and use ModelImporter.
e.g.
ModelImporter importer = assetImporter as ModelImporter;
ModelImporterClipAnimation clip = new ModelImporterClipAnimation(); clip.firstFrame = 0; clip.lastFrame = 1000; clip.loop = true; clip.name = "Test";
ModelImporterClipAnimation[] clips = new ModelImporterClipAnimation[] { clip };
importer.clipAnimations = clips;
Of course, you can add as many clips as you want.
Answer by Lucas Meijer 1 · Dec 21, 2009 at 08:08 AM
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.
The problem with that is that AddClip is horrendously slow and chews up a whole heap of memory, which is why I'm doing this. See http://answers.unity3d.com/questions/1658/adding-animation-clips-via-script
I'll try what you suggested though.
I tried doing:
AssetDatabase.CreateAsset(new AnimationClip(), "TempClip.asset"); AnimationClip clip = AssetDatabase.LoadAssetAtPath("TempClip.asset", typeof(AnimationClip)) as AnimationClip; Animation.AddClip(clip, "$$anonymous$$yNewAnimation");
but it fails at the first step, reporting that it cannot create the asset.
Reading the original problem: don't go for the CreateAsset(), ins$$anonymous$$d when you import your fbx, scan your filesystem (System.IO.*) for existing animations. Do a LoadAssetAtPath() on them, and AddClip() those.
Answer by runevision · Dec 21, 2009 at 09:47 AM
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.
I'm using Unity iPhone 1.5.1. That function doesn't appear to be there. Also, even if it where, how would you create the AnimationClips to pass to it?
Your answer
Follow this Question
Related Questions
Can the animation editor create local rotational data? 3 Answers
Adding animation clips via script 2 Answers
Can I make animations snap to a frame? 1 Answer
How to select an animation clip by index number? 7 Answers
Stop Movement while attacking 2 Answers