In our game the effects are created with animator and particle system. The reason we use the animator instead of animation component is that the animation component can no longer play material animations, and Unity decided that was designed Unity Issue Tracker - Can't animate color value of materials. So when I say animator, that means an animator component contains a controller which simply has one state of an animation clip.
The problem with Animator is that it Initialize every time when it’s enabled. I’ve already made pooling system for our effects to play, but still every time the effect is activated, all it’s Animator components do Initialize once again. AND IT’S REALLY SLOW! I can understand that initializing takes time, there’ll be states to create and properties to fetch, but I can’t understand why it does EVERY TIME.
Here I show you a profiler graph that I activated 20 effects at the same time.
Noticed that every time the effect is activated, the Animator spend huge time to Initialize.
Then I try to get rid of Animator components.
Because the only reason I use Animator is because Animation can’t animate materials, I decide to write a custom material animation component alone with a tool to peel off material animations from the original animation clip and record those curves in my custom material animation component. In that way, I can create new effects with Animation components to play non-material animations and custom material animation components to play material animations from old effects.
I almost jump up when I finish that tool and see the new effects play the same as the old effects, until I see the result on profiler.
The first part is new effect without Animator, the later part is the old one. Notice the Animator.Update of old effect is trade by MonoBehaviour.Update in new effect’s blue area, and the huge cpu spike created by Animator.Initialize is eliminated!
The problem is that Render take so much more time than the old effect, and the result would be worse on low end devices (this one is tested on iPhone 6).
In my custom material animation script, I use MaterialPropertyBlock to animate the material. The new effect’s SetPass calls, Batches and Saved by batching statistics are same as the old one shows in Editor Stats. I just don’t understand why there’s such significant slow down in rendering.
That just brings me back to the original place. The lost of Rendering performance is just unacceptable.
Now I just put two questions here hoping someone can enlighten me.
1.Why Animator.Initialize runs every time it’s enabled. How can I pass by it.
2.Why materials animated by Animator have much more rendering performance than animated by script.