1.animator.GetCurrentAnimatorStateInfo(0);
//参数是表示层级,返回的当前层级正在播放的动画的状态机(Base为0,New为1,以此类推)
(如果要获取不受缩放影响的原始剪辑,可以用runtimeAnimatorController.animationClip或者GetCurrentAnimatorClipInfo,区别在于一个能取到全部,一个只能取到当前播放的)
using System;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
private void Start()
{
if(gameObject.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("a"))
{
Debug.Log("c");
}
}
}
2.Animator.GetCurrentAnimationClipState 获取当前动画剪辑状态
获得当前层 正在播放 的剪辑数组,并且获取到的时间长度(clip.length)不受Animator速度影响。
如果想获取全部原始剪辑,可以用runtimeAnimatorController.animationClip
JavaScript
=> GetCurrentAnimationClipState(layerIndex: int): AnimationInfo[];C#
=> AnimationInfo[] GetCurrentAnimationClipState(int layerIndex);
Parameters 参数
layerIndex | The layer's index. 该层的索引。 |
Description 描述
Gets the list of AnimationInfo currently played by the current state.
获取当前状态播放的当前动画信息列表。
Animator m_Animator;
string m_ClipName;
AnimatorClipInfo[] m_CurrentClipInfo;
float m_CurrentClipLength;
void Start()
{
//Get them_Animator, which you attach to the GameObject you intend to animate.
m_Animator = gameObject.GetComponent<Animator>();
//Fetch the current Animation clip information for the base layer
m_CurrentClipInfo = this.m_Animator.GetCurrentAnimatorClipInfo(0);
//Access the current length of the clip
m_CurrentClipLength = m_CurrentClipInfo[0].clip.length;
//Access the Animation clip name
m_ClipName = m_CurrentClipInfo[0].clip.name;
}
void OnGUI()
{
//Output the current Animation name and length to the screen
GUI.Label(new Rect(0, 0, 200, 20), "Clip Name : " + m_ClipName);
GUI.Label(new Rect(0, 30, 200, 20), "Clip Length : " + m_CurrentClipLength);
}
3.使用Animation组件,不过不提倡,因为日常开发还是Animator Controller居多。很简单。
public Animation anim;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.R)) {
anim.Play("Run");
}
if (Input.GetKeyDown(KeyCode.G)) {
Debug.Log("正在播放的动画名为:"+ GetAnimationClipName());
}
}
public string GetAnimationClipName() {
foreach (AnimationState a in anim)
{
if (anim.IsPlaying(a.name))
{
return a.name;
}
}
return null;
}