Unity Animator 之 两种方法暂停继续播放动画

2025-10-06 00:48:37

1、Animator.speed:

The playback speed of the Animator. 1 is normal playback speed.

Use Animator.speed to manipulate the playback speed of the Animator. Any animations currently being played by the Animator are slowed down or sped up depending on how the speed is altered. Set speed to 1 for normal playback. Negative playback speed is only supported when the recorder is enabled. For more details refer to Animator.recorderMode.

2、方法提要:

1)方法一

animator.speed = 0;

animator.speed = 1;

2)方法二

Time.timeScale = 0;

Time.timeScale = 1;

1、打开Unity,新建一个空工程,具体如下

Unity Animator 之 两种方法暂停继续播放动画

2、导入一个带动画的游戏模型,并把游戏模型拖到场景中,再新建一个“Plane”,调整布局,具体如下图

Unity Animator 之 两种方法暂停继续播放动画

3、在工程中新建一个“Animator Controller”,然后在“Animator”窗口下,把模型的动画拖进状态树,具体如下图

Unity Animator 之 两种方法暂停继续播放动画

Unity Animator 之 两种方法暂停继续播放动画

4、把新建号的“AnimatorController”拖给场景中的模型的“Animator”组件上,具体如下图

Unity Animator 之 两种方法暂停继续播放动画

5、新建一个脚本“AnimatorTest”,双击脚本或者右键“Open C# Project”打开脚本,具体如下图

Unity Animator 之 两种方法暂停继续播放动画

6、在打开的“AnimationTest”脚本上编写代码,首先设置变量,一个获得“Animator”组件,然后设置按下不同键来实现动画的暂停和继续播放,两种方法,一种speed,一种timescale,代码及代码说明如下图

Unity Animator 之 两种方法暂停继续播放动画

7、“AnimatorTest”脚本具体了内容如下:

using UnityEngine;

public class AnimatorTest : MonoBehaviour {

    public Animator animator;

// Update is called once per frame

void Update () {

        if (Input.GetKeyDown(KeyCode.S)) {

            animator.speed = 0;

        }

        if (Input.GetKeyDown(KeyCode.C)) {

            animator.speed = 1;

        }

        if (Input.GetKeyDown(KeyCode.A)) {

            Time.timeScale = 0;

        }

        if (Input.GetKeyDown(KeyCode.B)) {

            Time.timeScale = 1;

        }

    }   

}

8、脚本编译正确,回到Unity界面,在场景中新建一个“GameObject”,把脚本“AnimatorTest”赋给“GameObject”,并把模型的“Animator”赋给脚本,具体如下图

Unity Animator 之 两种方法暂停继续播放动画

9、运行场景,通过不同的两种方法,实现了“Animator”动画的暂停播放,具体如下图

Unity Animator 之 两种方法暂停继续播放动画

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢