UnityAnimator之两种方法暂停继续播放动画。在Unity游戏中,暂停状态树动画的方式,本节介绍两种暂停动画,然后继续播放的方法的简单案例,具体如下
工具/原料
Unity
Animator
一、知识要点
1、Animator.speed:The咯悝滩镞playbackspeedoftheAnimator.1isnormalplaybackspeed.Use惺绅寨瞀Animator.speedtomanipulatetheplaybackspeedoftheAnimator.AnyanimationscurrentlybeingplayedbytheAnimatoraresloweddownorspedupdependingonhowthespeedisaltered.Setspeedto1fornormalplayback.Negativeplaybackspeedisonlysupportedwhentherecorderisenabled.FormoredetailsrefertoAnimator.recorderMode.
2、方法提要:1)方法一animator.speed=0;animator.speed=1;2)方法二Time.timeScale=0;Time.timeScale=1;
二、Animator之两种方法暂停继续播放动画
1、打开Unity,新建一个空工程,具体如下
2、导入一个带动画的游戏模型,并把游戏模型拖到场景中,再新建一个“Plane”,调整布局,具体如下图
3、在工程中新建一个“AnimatorController”,然后在“Animator”窗口下,把模型的动画拖进状态树,具体如下图
4、把新建号的“AnimatorController”拖给场景中的模型的“Animator”组件上,具体如下图
5、新建一个脚本“AnimatorTest”,双击脚本或者右键“OpenC#Project”打开脚本,具体如下图
6、在打开的“AnimationTest”脚本上编写代码,首先设置变量,一个获得“An坡纠课柩imator”组件,然后设置按下不旎髂坑若同键来实现动画的暂停和继续播放,两种方法,一种speed,一种timescale,代码及代码说明如下图
7、“AnimatorTest鹁鼍漉糍”脚本具体了内容如下:usingUnityEngine;publicclassAnimato鸡堕樱陨rTest:MonoBehaviour{publicAnimatoranimator; //Updateiscalledonceperframe voidUpdate(){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界面,在场景中新建一个“G瞢铍库祢ameObject”,把脚本“AnimatorTest”赋给“GameObject”,并把模型的“Animator”赋给脚本,具体如下图
9、运行场景,通过不同的两种方法,实现了“Animator”动画的暂停播放,具体如下图