多语言展示
当前在线:500今日阅读:23今日分享:31

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

Unity Animator 之 两种方法暂停继续播放动画。在Unity游戏中,暂停状态树动画的方式,本节介绍两种暂停动画,然后继续播放的方法的简单案例,具体如下
工具/原料
1

Unity

2

Animator

一、知识要点
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;

二、Animator 之 两种方法暂停继续播放动画
1

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

2

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

3

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

4

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

5

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

6

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

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”赋给脚本,具体如下图

9

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

注意事项
1

使用“timescale”来暂停动画的时候,切记“Animator”的“Update Mode”不能设置为“Unscaled Time”

2

您的支持,是我们不断坚持知识分享的动力,若帮到您,还请帮忙投票有得;若有疑问,请留言

推荐信息