How to get current state name on Mecanim?

Hi.

How to get current state name on Mecanim?
Is there a better solution than using AnimatorStateInfo.IsName() or AnimatorStateInfo.nameHash?

Thanks in advance!

For performance reasons you should NOT use the names at runtime. Comparing an integer is MUCH faster than a string comparison. However, that said, it can be nice thing so have the name of a state for debugging.

What we are did in our project is to export the whole controller graph to XML with the editor. At runtime that XML is loaded again and allows us to map hashes back to names. We also have information about state machines, so be can easily check if are currently in some child state of a certain StateMachine, etc. Checkout Editor API docs here: Animations.AnimatorController
I think we tied it in as an AssetModificationProcessor to generate the XML automatically whenever the controller graph changed.

If your controller graph is not that big and not that often changing, I would recommend to just build a Dictionary mapping fullPathHash back to the name. Something like this (untested). Note the use of #if UNITY_EDITOR to make sure that stuff is not used in a real build.

#if UNITY_EDITOR
public class DebugAnimationStates
{

    private Dictionary<int, string> m_states = new Dictionary<int, string>();

    public void Init()
    {
        Add("Base.Attack");
        Add("Base.Dodge");
        Add("Base.Walk");

        // ...

    }

    public string Get(int _fullPathHash )
    {
        string name;
        if (m_states.TryGetValue(_fullPathHash, out name))
            return name;

        return "Unknown#" + _hash;
    }

    private void Add(string _stateName)
    {
        int hash = Animator.StringToHash(_stateName);
        m_states.Add(hash, _stateName);
    }
}
#endif

var CurrStateName = “”;

function LateUpdate(){
    var anim = GetComponent(Animator);

    if( anim.GetCurrentAnimatorStateInfo(0).IsName("WALK")) { CurrStateName = "WALK"; }
    else if( anim.GetCurrentAnimatorStateInfo(0).IsName("RUN")) { CurrStateName = "RUN"; }
    else if( anim.GetCurrentAnimatorStateInfo(0).IsName("JUMP")) { CurrStateName = "JUMP"; }
    else{ CurrStateName = "IDLE"; }
}

I think your second solution is a good one as written here (but I never compare both) :

@Blubberfisch Thanks for answer, but I long ago came up with a solution)
I did to export controller graph data to prefab instead XML.
My solution for unity3d 4:
http://unity3d.ru/distribution/viewtopic.php?f=13&t=14440

I came up with an another solution for unity3d 5:
http://unity3d.ru/distribution/viewtopic.php?f=105&t=31486#p205103
There add StateMachineBehaviour scripts to a states and to a controller for getting data from any state. There you need to set manually the desired data in each state.

For performance reasons you should NOT use the names at runtime.
Yes, I know. But I have no problem with performance. This is often surplus optimization for animations (in my judgment) because that such checks are rarely invoked.

There’s a plugin on the asset store that lets you have access to the current state name. It can also be used to trigger events on Animator States with custom parameters, preview animator state in the scene, filter states more easily and much more!

Here’s the link: