Unity动画篇:Mecanim动画系统重点整合(获取当前正在播放的动画片段)

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 参数

layerIndexThe 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;       

    }

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值