- Home /
Is there a way to optimize animators?
Hi everyone, I have a 2D scene that has literally hundreds of gameobjects, each with an animator. When I go to the profiler, Animators.Update is taking up pretty much all of my CPU cycles. Now, I'm curious if there is a better way to do this, and if anyone knows...
I have changed all of the animators to CullCompletely, so they are only active when on screen. From what I have heard, using "Animations" rather than Animators is much more efficient, but I've also heard that you cannot use an "Animation" in 2d unless it goes into an animator.
Can anyone give me some guidance on where to go? Or is this a limitation of Unity?
Thanks!
Answer by RF008 · Jul 18, 2017 at 02:48 PM
i had the same issue with my game i have simply check if the animation game object is visibile to player
if visbile play animation or else disable animator compoenet.
I ended up using the same tactic...CullCompletely certainly does not stop the CPU cycles like disabling the animator...I went from 70% CPU usage to 40% when I switched to CullCompletely. Then when I disabled the animators that weren't within a certain distance of the player, it went down to less than 10%...Amazing how much CPU the Animator $$anonymous$$ecanim uses...
Answer by theANMATOR2b · Jul 17, 2017 at 08:05 PM
This will have to be tested for your specific game, because it depends..
Have you attempted to reduce animation frames in any of your imported animations? I don't know if that would help reduce overall cycles, but doesn't hurt to try.
Probably can reduce animated frames quite a bit, don't need a 30 fps animated swaying environment object.
Also might consider a tweening solution for things that can be 'tweened' instead of animations. Will have to be tested to see what is more optimal.
This may have worked, but I ended up just disabling the animators (seemed like a simpler approach to start out with). I appreciate the response though!
Answer by Shinugami · Aug 12, 2017 at 02:36 PM
The main way is to clamp values that are almost zero down to zero. This prevents multiple blends from being applied when it's not even noticeable to the player.
After that, I would recommend making an AI / Management script that sits in your scene and iterates through the active objects. Based on their distance, zone, visibility, activity etc you could change how much of their behavior is active and determine whether their animations are on or off. Culling is good but if you have animation events, such as footsteps, then you would need to be creative with how you play footsteps that are on culled AI. This is why I prefer to make my own culling method because the Unity Cull is not always ideal.
Think about what makes your AI inactive || active and set up a logic check to see what level of detail it qualifies for. If an AI is far from the player, legacy animations will be faster but this means you need to make a script to deal with legacy and one to deal with animator animations. Just make your game modular and disable objects that are not needed.
The management scripts are not too heavy, just set a cooldown for X amount of time between each iteration.Everytime the cooldown reaches zero, have the management script iterate through the active/inactive AI and determine what each AI should be set to. If it's heavy then you just increase the cooldown. Works well for me.