Unity-- Animator animation method call

Play on the animation, all animation obtain the name of the object's current name and length of time

    private Animator animator;
    private AnimationClip[] animationClip;
    void Start()
    {
        animator = GetComponent<Animator>();
        animationClip = animator.runtimeAnimatorController.animationClips;
        Debug.Log("该对象有" + animationClip.Length + "个动画");
        foreach(AnimationClip a in animationClip)//遍历获取所有该对象的动画名
        {
            Debug.Log(a.name);
        }
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            Debug.Log("按下A");
            animator.Play(animationClip[0].name);   //播放动画
            animator.Update(0);         //刷新0层的动画,默认新建的动画在0层。
            GetAnimatorInfo();
        }
        if (Input.GetKeyDown(KeyCode.B))
        {
            Debug.Log("按下B");
            animator.Play(animationClip[1].name);
            animator.Update(0);
            GetAnimatorInfo();
        }
    }
    void GetAnimatorInfo()
    {
        string name = animator.GetCurrentAnimatorClipInfo(0)[0].clip.name;//获取当前播放动画的名称
        Debug.Log("当前播放的动画名为:" + name);
        float length = animator.GetCurrentAnimatorClipInfo(0)[0].clip.length;//获取当前动画的时间长度
        Debug.Log("播放动画的长度:" + length);
    }

Output display:

Play and Pause

There are two ways to play

animator.speed = 1; // animation speed is 1, i.e., play

Or animator.enabled = true; // Play

Pause the same

animator.speed = 0; // Animation speed is zero, that is, suspended

Or animator.enabled = false; // Pause

The animation

animator.Play ( "name", 0, 0.5f); // parentheses indicate the following: name of the animation to play, animation layer where, I hope what period of time from the start of play.

animator.Play ( "name"); the same, but where the layer and play position defaults to 0.

Published 28 original articles · won praise 14 · views 40000 +

Guess you like

Origin blog.csdn.net/Ro969668074/article/details/104673725