Unity3D: Reusing animator controllers with AnimatorOverrideController
I've decided to start writing tutorials for features of Unity3D. If there is something you'd like me to write about, feel free to add it in the comments below, or simply contact me. Today we will be looking at AnimatorOverrideController.
You finally reach the point in your project's development where you need to import a 3D model and animate it. You get the model and create your first Animator Controller.
You then go back to the code and make the model animate in-game. At some point you'd like to add a new model. There's nothing special about it other than how it looks and animates. When and why those animations are triggered are exactly the same as the previous model. So, to avoid writing any special code, you make sure that the new Animator Controller has the same parameters and state transitions as the first.
You integrate the second model into the game and soon comes the third.
Now it's becoming obvious that you are spending too much time on each model, and you'll spend even more time if the state machine needs to change. And that's where the AnimatorOverrideController comes in.
Let's first delete all but one Animator Controllers. Probably rename it into something generic since we'll be re-using it for multiple models. We then create a new Animator Override Controller for each of our other models (Assets -> Create -> Animator Override Controller).
The first step in each new Animator Override Controller is to set which Animator Controller it will be overriding. We simply drag & drop the only one we have and as soon as that happens, we see a list of all the states in that Animator Controller. We need to then drag & drop all the animation takes from our model to their respected state name.
Finally, in the Animator component of our model, we now set the controller field to the override controller.
And now we're set. All Animator components use the same Animator Controller with different animations for each of our models.
But what happens when you do not override a particular state? Well, if the two models share the same skeleton, (as in, all bones have the same name), then the model that contains the override will be playing the animation of the original model. And we can actually take advantage of this and group a bunch of models together that will all use the same override. And then group a bunch of groups of models that will all use the same animator.
For example, let's say we have Humans, Elves and Dwarfs. We are lazy as can be, so, as far as the code is concerned, we want all of them to have the exact same states in their Animator Controller. So we create a single one called humanoid_animations. Our artists are equally lazy, so they create a single skeleton for all the Humans. They animate that skeleton for one model, and then use the same one when they rigged the rest of them. They do the same thing for Elves and Dwarfs. This is very convenient for us, because now, all we have to do is create only three Animator Override Controllers, one for each type of humanoid, and set them to the appropriate models. Bam! For around 30 models, we ended up having a single Animator Controller and only 3 Animator Override Controllers.
Lazy might be the wrong word.
Did you know?
Until you hit Save Scene or Save Project, all changes to your prefabs are kept in Unity's memory. This is very important when using a version control system as you will likely not commit those changes if you forget to save. Also, you'll lose those changes when Unity crashes. So save often!