從上面這張圖可以得到目前這個Animator有兩個狀態互相切換,各自有各自的名稱(anim跟anim2),同時左上角可以得到這個Layer的名稱(Base Layer)。
然後直接使用狀態資訊來比對就好,只是名稱不能用string來比對而已。
Code說明
public Animator animator;
void Update () {
//先取得目前狀態的資訊
AnimatorStateInfo currentState = animator.GetCurrentAnimatorStateInfo(0);
//然後直接比對名稱就好,但是狀態資訊中並不是用string來存,所以把名稱轉Hash來比對,格式[Layer name].[state name]
if(currentState.nameHash == Animator.StringToHash("Base Layer.anim")) {
Debug.Log("This is Anim 1");
}
if(currentState.nameHash == Animator.StringToHash("Base Layer.anim2")) {
Debug.Log("This is Anim 2");
}
//或是先定義一個參數
public int myState = Animator.StringToHash("Base Layer.anim");
if(currentState.nameHash == myState ) {
Debug.Log("This is Anim 1");
}
}

No comments:
Post a Comment