Owe*_* D. 3 c# unity-game-engine
如何在Animator组件的图层中获取当前状态的名称?我意识到我可以比较名称,GetCurrentAnimatorStateInfo(0).IsName("statename")但我不想为我的图层中的每个状态运行它.是否可以简单地获取当前状态的名称?
我不认为这是可能的.我能想到的唯一好方法是使用switch语句,nameHash如下所示:
Animator animator = GetComponent<Animator>();
// Get the id of all state for this object
int runId = Animator.StringToHash("Run");
int jumpId = Animator.StringToHash("Jump");
AnimatorStateInfo animStateInfo = animator.GetCurrentAnimatorStateInfo(0);
switch (animStateInfo.nameHash)
{
case runId:
Debug.Log("Current state is Run");
break;
case jumpId:
Debug.Log("Current state is Jump");
break;
default:
Debug.Log("Current state is not in this list");
break;
}
Run Code Online (Sandbox Code Playgroud)
我们可以使用以下代码获取图层中当前播放的剪辑名称
private Animator animator = GetComponent<Animator>();
private AnimatorClipInfo[] clipInfo;
public string GetCurrentClipName(){
clipInfo = animator.GetCurrentAnimatorClipInfo(0);
return clipInfo[0].clip.name;
}
Run Code Online (Sandbox Code Playgroud)
注意:此代码在 2018.3 统一版本中进行了测试,所以我不确定这是否适用于以前的版本。