位置: 编程技术 - 正文
推荐整理分享Unity 编辑器下控制播放Animator(unity 控件),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:unity ui控件,unity 编辑器插件,unity控制器怎么打开,unity-control-center,unity ui控件,unity 控件,unity 控件,unity编辑器设置,内容如对您有帮助,希望把文章链接给更多的朋友!
在Unity编辑器的Scene视图进行控制播放Animator,可以借助方法StartPlayback、StopPlayback、StartRecording和StopRecording,具体可以参照以下例子:
创建一个带Controller的Animator对象,可以看到此时人物呈现T型姿势。附上新脚本EditAnimator,此为空脚本,如下: C# Code using UnityEngine;public class EditAnimator : MonoBehaviour {}创建一个这个脚本的编辑器类EditAnimatorInspector,代码如下: C# Code using UnityEditor;using UnityEngine;[CustomEditor(typeof(EditAnimator))]public class EditAnimatorInspector : Editor{ /// <summary> /// 滑动杆的当前时间 /// </summary> private float m_CurTime; /// <summary> /// 是否已经烘培过 /// </summary> private bool m_HasBake; /// <summary> /// 当前是否是预览播放状态 /// </summary> private bool m_Playing; /// <summary> /// 当前运行时间 /// </summary> private float m_RunningTime; /// <summary> /// 上一次系统时间 /// </summary> private double m_PreviousTime; /// <summary> /// 总的记录时间 /// </summary> private float m_RecorderStopTime; /// <summary> /// 滑动杆总长度 /// </summary> private const float kDuration = f; private Animator m_Animator; private EditAnimator editAnimator { get { return target as EditAnimator; } } private Animator animator { get { return m_Animator ?? (m_Animator = editAnimator.GetComponent<Animator>()); } } void OnEnable() { m_PreviousTime = EditorApplication.timeSinceStartup; EditorApplication.update = inspectorUpdate; } void OnDisable() { EditorApplication.update -= inspectorUpdate; } public override void OnInspectorGUI() { 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> private void bake() { if (m_HasBake) { return; } if (Application.isPlaying || animator == null) { return; } const float frameRate = f; const int frameCount = (int)((kDuration * frameRate) 2); animator.Rebind(); animator.StopPlayback(); animator.recorderStartTime = 0; // 开始记录指定的帧数 animator.StartRecording(frameCount); for (var i = 0; i < frameCount - 1; i) { // 这里可以在指定的时间触发新的动画状态 if (i == ) { animator.SetTrigger("Dance"); } // 记录每一帧 animator.Update(1.0f / frameRate); } // 完成记录 animator.StopRecording(); // 开启回放模式 animator.StartPlayback(); m_HasBake = true; m_RecorderStopTime = animator.recorderStopTime; } /// <summary> /// 进行预览播放 /// </summary> private void play() { if (Application.isPlaying || animator == null) { return; } bake(); m_RunningTime = 0f; m_Playing = true; } /// <summary> /// 停止预览播放 /// </summary> private void stop() { if (Application.isPlaying || animator == null) { return; } m_Playing = false; m_CurTime = 0f; } /// <summary> /// 预览播放状态下的更新 /// </summary> private void update() { 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> private void manualUpdate() { if (animator && !m_Playing && m_HasBake && m_CurTime < m_RecorderStopTime) { animator.playbackTime = m_CurTime; animator.Update(0); } } private void inspectorUpdate() { var delta = 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",即可以自动播放整个动画,如下所示:unity 写批量打包工具时遇到的一个问题 上一篇文章解决了打出来的xcode工程不能append的问题(其实这个问题也解决了)。但是根据一些渠道要求是需要替换游戏Icon和SplashImage的,而defaultIcon和M
NullReferenceException: Object reference not set to an instance of an object 初学unity,遇到问题总是忙来忙去半天找不到解决方法,今天又遇到一个比较坑的问题,记录一下。。voidUpdate(){//KeyCode1:if(Input.GetKeyDown(KeyCode.X)){getIdShow(
【Unity】减少GC回收 两种情况GC会被触发:首先当然是我们的堆的内存不足时,会自动调用GC。其次呢,作为编程人员,我们自己也可以手动的调用GC。减少GC回收要注意一下
标签: unity 控件
本文链接地址:https://www.jiuchutong.com/biancheng/381468.html 转载请保留说明!友情链接: 武汉网站建设