A Unity tool that auto-generates files containing cached, type-safe variables for the Animator Controller parameters in your project. Basically, it prevents you from having to write this sort of boilerplate over and over.
Since Animator Controller parameters are string-based, it's really easy to introduce typos into your code. It can cost hours of bug hunting when an animation isn't playing the way it should, only to discover that the string in your code doesn't match the name of the Animator Controller parameter. We've all been there...
Which brings us to the Animator Hash Generator. This tool scrapes the parameters from the given Animator Controllers and generates a script that caches all the strings into convenient, type-safe variables. Additionally, the tool uses Animator.StringToHash() to store the values as integers to help squeeze a little extra performance out of your animation code.
To open the Animator Hash Generator, go to Tools > Generate Animator Hashes...
A window will open that looks like this:
The section on the right under the "Settings" tab lets you define which Animator Controllers you want to include in your generated file. There are two options:
This option allows you to choose a folder that contains all the Animator Controllers you want to include in your file. All sub folders will also be included in the search.
This option allows you to define a list of specific Animator Controllers you want to include. You can multi-select them from your project view and drop them in the box to add them to the list.
The "Saving" section lets you choose where your generated file will be stored in your project.
The Formatting section contains various options for controlling the formatting of the generated variable names. You can mess around with these settings until you find something that suits your preferences. You can preview how these settings affect the variable names in the help boxes.
NOTE: In order for the formatting to be properly applied to the variable names, it is assumed that your Animator Controller properties use camel case and your layers use spaces (following the formatting of the built-in "Base Layer").
In the left hand column is a list of presets. You can add or remove presets as needed. This allows you to easily create different settings and save locations for different groups of Animator Controllers in your project.
Once you generate a file, the class will look something like this (depending on your formatting settings):
public static class AnimHashIDs
{
//TestController_01
public static readonly int WALK_TRIGGER = Animator.StringToHash ("walk");
public static readonly int IS_WALKING_BOOL = Animator.StringToHash ("isWalking");
public static readonly int WALK_SPEED_FLOAT = Animator.StringToHash ("walkSpeed");
public static readonly int WALK_VARIATION_INT = Animator.StringToHash ("walkVariation");
public static class Layers
{
//TestController_01
public static readonly string BASE_LAYER = "Base Layer";
public static readonly string LEGS = "Legs";
public static readonly string TORSO = "Torso";
}
}
Then in your animation code you can reference these variables easily like so:
public class MyAnimationClass : MonoBehaviour
{
[SerializeField]
private Animator _Animator;
private int _legsLayerID;
void Awake ()
{
_legsLayerID = _Animator.GetLayerIndex (AnimHashIDs.Layers.LEGS);
}
public void Walk (float speed, int variation)
{
_Animator.SetBool (AnimHashIDs.IS_WALKING_BOOL, speed > 0f);
_Animator.SetFloat (AnimHashIDs.WALK_SPEED_FLOAT, speed);
_Animator.SetInt (AnimHashIDs.WALK_VARIATION_INT, variation);
_Animator.SetLayerWeight(_legsLayerID, 1f);
}
}
The system will detect any duplicates and only generate a hash variable for the first parameter with a given name.