animator.StringToHash() expansive at runtime?

Hi all,

I was wondering if it could be expensive to call animator.StringToHash() every time it is needed. Or would it be wiser to first save the hash value into an integer?

private var attackHash : int;
attackHash = animator.StringToHash("BaseLayer.attack"); 

and then use

if( animatorInfo.nameHash == attackHash ) {}

or is it ok to just use

if( animatorInfo.nameHash == animator.StringToHash("BaseLayer.attack") ) {}

I wan’t to use multiple AI’s using mecanim, not only one. How would it impact performance on a large scale?

Thank you,


Hashing a string is not crazy expensive, but it’s wasteful to recompute it all the time. Most Unity games are main thread CPU bound, and if you allow yourself to write wasteful bits of code everywhere, you’ll find yourself in a “death of a thousand” cuts scenario where your game is slow, but hard to optimize because everything is inefficient, not just one or two big hotspots.