- Home /
Adding animation clips via script
I'm having a speed/memory problem when adding animations via script.
I have a character model that has 100+ animations spanning around 1800 frames. I've had trouble with Unity occasionally deleting all my animation clips when I reimport models, so I decided to add the animation via script, using animation.AddClip(...).
So now I have around 100 animation.AddClip(...) calls inside that character's Awake method, and on top of that I have up to 6 of these characters in a single scene.
The problem is that, on a 3GS, the game takes around 8 seconds to load those clips per character and also takes an addition 4-5MB, which kills older devices.
One way I could attempt to fix this is by just adding the animation clips in the Asset Import window like Unity expects you to, but I don't want to do this unless it's going to actually fix things, and there's also the side issue of us having a few different character models, all with the same animation frames; so I'd rather not have to duplicate everything for each one, but will do if it's our only option.
So my question is, will adding the clips via Asset Import fix my problem (and why?) and is there any better way to manage my situation.
Thanks in advance.
Answer by Jaap Kreijkamp · Dec 20, 2009 at 11:52 PM
If you don't have the performance issues when the clips are added through the inspector, you could try to add them using an editor script so you don't have to redo that every time when losing the connections after an import.
But, 100+ animations? It's quite a lot, don't know what you're doing but possibly you could reduce the amount by blending animations (separating the lower-body from the upper-body animations).
Thanks, I'll try the editor script.
Unfortunately there's not really any way we can reduce the number of animations, we simply need that many. A few of them could be blended, but there's still many more to add, so there's going to have to be some way of getting around it either way.
Answer by nightstalker420 · Feb 03, 2013 at 02:11 PM
I had that problem on Android. The way I solved it was to not use animation compression and only call the animations (by script) as they were needed (duplicate, then delete the imported animations).
The script had to use Mathf.ABS rather than just Input.GetAxis and attached the script to the Tank rather than using Search("Player"). I also moved all the animations, after duplicated, to their own folder away from the models folder.
if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
animation.CrossFade("rotateLeftAnimation");
else if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0.1)
animation.CrossFade("forwardAnimation");
else
animation.CrossFade("idleAnimation");
rather than just if (Input.GetAxis("Vertical")) > 0.1)
I posted a script yesterday that would handle all the animations and allow the private var so you can select each animation for each event here: http://answers.unity3d.com/questions/382738/how-to-start-in-animating-my-model.html
Your answer
Follow this Question
Related Questions
Android Performance with 2d BoneBased Animation 0 Answers
How to select an animation clip by index number? 7 Answers
How to properly setup kinematic rigid bodie(s) and colliders in animated human character 0 Answers
Removing scale curves from animation 1 Answer
Does an animator without an animation controller set (None) affect performance? 1 Answer