I was having some doubts either to post this in the 2D or the Animation forum because my problems are related to both of them at the same time, but decided to write it here because they serve no purpose for 3D animation (except for the 2nd listed below).
So, I’m creating a plugin that will extract 2D bone animation data from a text file and create the animations inside unity as .anim so that I can use the new 2D Mecanim integration.
I’ll list the problems I’ve faced until now. For some of them I found a workaround. They’re all related to creating an animation clip via scripting.
1-> Procedurally created Animation Clip cannot be used in Animator Controller
After creating the animation clip in my script, when I attempted to use it in an Animator Controller I’d receive a warning message telling me that the animation clip could not be used because it contained no Muscle Definition and that I should change the Importer Settings to fix that.
Well, as far as I know, there’s no such thing for 2D animation clips and also there’s no animation Importer Settings if I’m creating it by code, right? But one of the new 2D features allow you to create a 2D animation clip that works on Animator Controller by dragging a couple of sprites to the “Hierarchy” panel. But there is no way, at least I have not found it, to set the muscle definition for an animation clip by code.
WORKAROUND: I’ve created an 2D animation clip the “drag’n’drop” way and cleared its curves, resulting in an “empty animation” asset. The settings needed for the Animation Controller (muscle definition, …?) are not lost this way. Then, for each animation that I need to create, I duplicate this “empty animation” and put my own curves on it. By doing that I’m able to create an animation clip via scripting and use it in the Animator Controller. Not the prettiest thing, I know, but it works, at least…
2-> Artifacts with the rotation curves
This is not really a lack of scripting functions but the lack of their documentation. I had some problems with rotation artifacts, but it has already been solved (http://forum.unity3d.com/threads/212198-Problem-with-rotation-artifacts-when-creating-animation-clip-procedurally) using the AnimationClip.EnsureQuaternionContinuity function.
3-> How to set a keyframe to change the sprite of a SpriteRenderer at a given time.
For what I’ve seen, the only data that you can input to an AnimationClip is AnimationCurves. The AnimationCurve is made of Keyframes and these are made of two floats, time and value. How could I set a keyframe so that at 3 seconds, the SpriteRenderer would change a sprite from “eyeOpen” to “eyeClose” if all I can do is set a float for a given time? This is a functionality that is available through the “Animation Window”, but I can’t find a way to do it via scripting.
To sumarize it all, I’m very happy with the new 2D features. They’re awesome and all, but I’m a little bit frustrated that there are so many features available through “drag’n’drop” in the editor that cannot be reproduced in scripts. Perhaps I’m doing it all wrong and this the “legacy” way of doing animation by code but this is the only way I was able to do it.
Thanks for reading and please let me know if you’ve faced this or similar problems and how you solved them.