Unity3d - How to get Animator's Layer -->Sub-State --> State namehash?

后端 未结 3 1864
北海茫月
北海茫月 2021-01-22 14:08

I\'m using the following structure:

(Layer) \"Base Layer\" --> (Sub-State) \"Jump_Fall_Roll\" --> (State) \"Roll\"

static int rollState = Animat         


        
相关标签:
3条回答
  • 2021-01-22 14:34

    The string to pass in Animator.StringToHash should include layer's name and state's name.

    So to correctly generate the hash it should be:

    int rollState = Animator.StringToHash("Base Layer.Roll");
    
    0 讨论(0)
  • 2021-01-22 14:42

    I seriously have no Idea what changed in my project, but I tryed

    static int rollState = Animator.StringToHash ("Jump_Fall_Roll.Roll");
    

    again (I had tested it before I post this question and spend hours trying to resolve it) and it managed to work..........

    So, for Sub-States, do not include the Layer name in the hash, only the parent Sub-State of your State.

    I did not test for nested Sub-States.

    Thank you to those who tryed to help me

    0 讨论(0)
  • 2021-01-22 14:43

    In the most up-to-date version of 5.3.4 at time of writing, the question can be partially answered as follows.

    First of all, there is no nameHash any more. The closest variable to it is shortNameHash. It is recommended to use fullPathHash instead as will be explained shortly. shortNameHash is like the bare file name in OS, while fullPathHash, as its name suggests, contains layer name as path before the state name. Here is an example to use both of them to identify the current state:

    int baseState = Animator.StringToHash("Base Layer.Idle");
    int short_baseState = Animator.StringToHash("Idle");
    int subState = Animator.StringToHash("Base Layer.Jump_Fall_Roll.Roll");
    int short_subState = Animator.StringToHash("Roll");
    
    ...
    
    AnimatorStateInfo s = myAnimator.GetCurrentAnimatorStateInfo(0);
    Debug.Log(s.fullPathHash + ", " + baseState);   //they are equal at Idle state in base layer
    Debug.Log(s.shortNameHash + ", " + short_baseState);   //they are equal at Idle state in base layer
    Debug.Log(s.fullPathHash + ", " + subState);   //they are equal at Roll state in sub-state machine
    Debug.Log(s.shortNameHash + ", " + short_subState);   //they are equal at Roll state in sub-state machine
    

    From the above example we can see that shortNameHash works for bare state name, no matter the state is regular one in base layer, or a state in the sub-state machine. But fullPathHash works only when layer path is properly prefixed before the state name. If we are sure there is no duplicate state names in different state machines, shortNameHash is good because it is short, but this assumption does not always hold in a large complex animator controller, so I would like to recommend using fullPathHash all the time.

    I call this answer "partial" because we have to make sure the layer id passed to GetCurrentAnimatorStateInfo is 0, i.e., the "current working layer" (like the pwd command in linux) should be the base layer. The above path name convention fails if we pass 1 to it. I don't have time to guess what the path name should be in layer 1. Any complement in this regard is welcomed.

    0 讨论(0)
提交回复
热议问题