This video gives a quick overview of Animator Access Generator, an open source C# code generator that makes handling of Animator states and parameters easier.

Note: This became deprecated with Unity3D version 5 with the introduction of State Machine Behaviours.

Aside from this it might be interesting because of some advanced techniques for code generation in editor scripts.

2-step approach when methods are renamed: Previous methods remain in generated code but marked as obsolete. Thus you can refactor your own code to avoid compiler errors On every run the code generator removes all methods marked as obsolete.So clean up is done by running code generation one more time.

You don’t have to enter state or parameter names manually any longer as they will be generated automatically.

Register event handlers on state changes or transitions that met predefined conditions.

Using it will let the compiler detect most possible problems in your code, saving you time consuming bug analysis because of wrong hash IDs.

Most important features:

Defines public member variables to hold the hash IDs of all Animator states and parameters and initialises them in Awake () with the correct name string constants

with the correct name string constants Query Animator states by using explicit methods e.g.

bool IsIdle ( ) for checking a state named Idle

for checking a state named Idle Provides dedicated methods for accessing every parameter e.g.

void SetSpeed ( float newValue ) and float GetSpeed () for setting a float parameter speed

and for setting a float parameter speed Register event handlers to be called on Animator state changes or transtions e.g.

anim . TransitionTo ( anim . stateIdWalking ). OnStarted += OnStartedTransitionToWalking ;

anim . State ( anim . stateIdIdle ). OnActive += OnIdle ;

Easy to use through custom editor for AnimatorAccess components



Supports smooth updates on name changes by analysing the existing class’s code and marks outdated members as obsolete first. They will not be removed before the code is generated one more time

Customise the way how variable and method names are built

Resources

Animator Access Generator is an open source project hosted at GitHub see:

Code Sample

Excerpt of generated code (see ExamplePlayerAnimatorAccess.cs for full code):

public class ExamplePlayerAnimatorAccess : BaseAnimatorAccess { /// <summary> /// Hash of Animator state Base Layer.Idle /// </summary> public int stateIdIdle; // ... public int paramIdSpeed; public int paramIdJumpTrigger; // ... public void Awake () { animator = GetComponent<Animator> (); // ... } public override void InitialiseEventManager () { StateInfos.Add (-2010423537, new StateInfo (-2010423537, 0, "Base Layer", "Base Layer.Walking", "", 1f, false, false, "Walk")); // ... TransitionInfos.Add (708569559, new TransitionInfo (708569559, "Base Layer.Walking -> Base Layer.Idle",. // ... } public bool IsIdle () { return stateIdIdle == animator.GetCurrentAnimatorStateInfo (0).nameHash; } // ... public void SetSpeed (float newValue, float dampTime, float deltaTime) { animator.SetFloat (paramIdSpeed, newValue, dampTime, deltaTime); } public void SetSpeed (float newValue) { animator.SetFloat (paramIdSpeed, newValue); } public float GetSpeed () { return animator.GetFloat (paramIdSpeed); } public void SetJumpTrigger () { animator.SetTrigger (paramIdJumpTrigger); } // ... }

Using generated code (see Player.cs for full code):

using AnimatorAccess; public class Player : MonoBehaviour { ExamplePlayerAnimatorAccess anim; Animator animator; int currentState0; public float speed; public float maxSpeed = 5f; float horizontalInput; bool jumpKeyPressed = false; void Awake() { animator = GetComponent<Animator>(); anim = GetComponent<ExamplePlayerAnimatorAccess>(); } void OnEnable() { anim.State(anim.stateIdIdle).OnActive += OnIdle; anim.State(anim.stateIdJumping).OnEnter += OnEnterJumping; anim.TransitionTo(anim.stateIdWalking).OnStarted += OnStartedTransitionToWalking; } void OnDisable() { anim.OnStateChange -= OnStateChange; } void OnStartedTransitionToWalking(TransitionInfo info, LayerStatus status) { Debug.Log("Called when any transiton with target walking is starting"); } void OnIdle(StateInfo info, LayerStatus status) { Debug.Log("OnIdle: called every frame while in state idle"); } void OnEnterJumping(StateInfo info, LayerStatus status) { Debug.Log("OnEnterJumping: Jump, called once "); } void Update() { horizontalInput = Input.GetAxis("Horizontal"); jumpKeyPressed = Input.GetKeyDown(KeyCode.UpArrow); } void FixedUpdate() { if (anim.IsJumping()) { // alternatively use polling to check for state changes instead of events } if (jumpKeyPressed) { anim.SetJumpTrigger(); } speed = horizontalInput * maxSpeed; anim.SetSpeed(Mathf.Abs(speed)); // alternative (not recommended) way of accessing hash IDs directly: // animator.SetFloat (anim.paramIdSpeed, Mathf.Abs (speed)); } }