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

后端 未结 3 1868
北海茫月
北海茫月 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: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.

提交回复
热议问题