Unity编辑器下如何控制播放Animator

Unity编辑器下如何控制播放Animator

这篇文章主要介绍Unity编辑器下如何控制播放Animator,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

在Unity编辑器的Scene视图进行控制播放Animator,可以借助方法StartPlayback、StopPlayback、StartRecording和StopRecording,具体可以参照以下例子:

创建一个带Controller的Animator对象,可以看到此时人物呈现T型姿势。附上新脚本EditAnimator,此为空脚本,如下:

usingUnityEngine;publicclassEditAnimator:MonoBehaviour{}

创建一个这个脚本的编辑器类EditAnimatorInspector,代码如下:

usingUnityEditor;usingUnityEngine;[CustomEditor(typeof(EditAnimator))]publicclassEditAnimatorInspector:Editor{///<summary>///滑动杆的当前时间///</summary>privatefloatm_CurTime;///<summary>///是否已经烘培过///</summary>privateboolm_HasBake;///<summary>///当前是否是预览播放状态///</summary>privateboolm_Playing;///<summary>///当前运行时间///</summary>privatefloatm_RunningTime;///<summary>///上一次系统时间///</summary>privatedoublem_PreviousTime;///<summary>///总的记录时间///</summary>privatefloatm_RecorderStopTime;///<summary>///滑动杆总长度///</summary>privateconstfloatkDuration=30f;privateAnimatorm_Animator;privateEditAnimatoreditAnimator{get{returntargetasEditAnimator;}}privateAnimatoranimator{get{returnm_Animator??(m_Animator=editAnimator.GetComponent<Animator>());}}voidOnEnable(){m_PreviousTime=EditorApplication.timeSinceStartup;EditorApplication.update+=inspectorUpdate;}voidOnDisable(){EditorApplication.update-=inspectorUpdate;}publicoverridevoidOnInspectorGUI(){EditorGUILayout.BeginHorizontal();if(GUILayout.Button("Bake")){m_HasBake=false;bake();}if(GUILayout.Button("Play")){play();}if(GUILayout.Button("Stop")){stop();}EditorGUILayout.EndHorizontal();m_CurTime=EditorGUILayout.Slider("Time:",m_CurTime,0f,kDuration);manualUpdate();}///<summary>///烘培记录动画数据///</summary>privatevoidbake(){if(m_HasBake){return;}if(Application.isPlaying||animator==null){return;}constfloatframeRate=30f;constintframeCount=(int)((kDuration*frameRate)+2);animator.Rebind();animator.StopPlayback();animator.recorderStartTime=0;//开始记录指定的帧数animator.StartRecording(frameCount);for(vari=0;i<frameCount-1;i++){//这里可以在指定的时间触发新的动画状态if(i==200){animator.SetTrigger("Dance");}//记录每一帧animator.Update(1.0f/frameRate);}//完成记录animator.StopRecording();//开启回放模式animator.StartPlayback();m_HasBake=true;m_RecorderStopTime=animator.recorderStopTime;}///<summary>///进行预览播放///</summary>privatevoidplay(){if(Application.isPlaying||animator==null){return;}bake();m_RunningTime=0f;m_Playing=true;}///<summary>///停止预览播放///</summary>privatevoidstop(){if(Application.isPlaying||animator==null){return;}m_Playing=false;m_CurTime=0f;}///<summary>///预览播放状态下的更新///</summary>privatevoidupdate(){if(Application.isPlaying||animator==null){return;}if(m_RunningTime>m_RecorderStopTime){m_Playing=false;return;}//设置回放的时间位置animator.playbackTime=m_RunningTime;animator.Update(0);m_CurTime=m_RunningTime;}///<summary>///非预览播放状态下,通过滑杆来播放当前动画帧///</summary>privatevoidmanualUpdate(){if(animator&&!m_Playing&&m_HasBake&&m_CurTime<m_RecorderStopTime){animator.playbackTime=m_CurTime;animator.Update(0);}}privatevoidinspectorUpdate(){vardelta=EditorApplication.timeSinceStartup-m_PreviousTime;m_PreviousTime=EditorApplication.timeSinceStartup;if(!Application.isPlaying&&m_Playing){m_RunningTime=Mathf.Clamp(m_RunningTime+(float)delta,0f,kDuration);update();}}}

检视器效果如下:

点击"Bake",可以看到此时人物姿势变成了动画状态的第一帧,如下所示:

拖动滑杆,即可播放当前时间的那一帧,如下所示:

点击"Play",即可以自动播放整个动画,如下所示:

最后,u3dnotes.com温馨提示:通过Unity3d的Wwindow->Animation窗口,可方便地在编辑器线下非运行时预览乃至编辑动画。xD

以上是“Unity编辑器下如何控制播放Animator”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

发布于 2022-01-05 23:13:03
收藏
分享
海报
0 条评论
140
上一篇:Java基础面试题是什么 下一篇:2022檀香木一吨价格(檀香木多少钱一公斤)
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码