I have a scene that has 2 characters in it, each with it’s own Animator and AnimatorController.
I want to be able to access the states of these Animators in my script using Animator.StringToHash() at runtime, but they both have the same layer names and the same state names within the layers. How do I specify which AnimatorController to get a hash value from if the method is static?
Late answer but since I’m here…
You can get the hash from the static method regardless of which animator controller you’re going to pass it to. If the layer and animation and everything are the same, you would get the same hashed value for both animation controllers anyway–you could even share the result between all instances of the class!
The animator controller that will play the animation is the one that you call Play on, passing the hash into.
It works because the list of hashes are stored in a static variable at the Class level. In that case, technically all layer names are available to (and shared across) all Animators. So querying for the hash’s index value is checking in the same pool of hash names, regardless of which Animator you are in.
Take this example:
"Base" = 0
"Foo" = 1
"Bob" = 2
If your first animation controller had layers "Base" and "Foo" and the second had "Base" and "Bob", getting the hash value for "Base" would always return 0, "Foo" would return 1 and "Bob" would return 2, regardless of which controller they were used in.
Anim Controller 1 would have hash indexes 0 & 1
Anim Controller 2 would have hash indexes 0 & 2
In a third controller had all three layers, it would have hash layers 0, 1 & 2.
In a fourth controller, if it just had "Bob" it would have hash layer 2
This only works because the Hash list is shared amongst all Animators.