In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to control the playback of Animator under the Unity editor, the article is very detailed, has a certain reference value, interested friends must read it!
You can control the playback of Animator in the Scene view of the Unity editor by using the methods StartPlayback, StopPlayback, StartRecording and StopRecording. For more information, please refer to the following example:
Create an Animator object with Controller, and you can see that the character takes a T-shaped pose. Attach the new script EditAnimator, which is an empty script, as follows:
Using UnityEngine;public class EditAnimator: MonoBehaviour {}
Create an editor class EditAnimatorInspector for this script, with the following code:
Using UnityEditor;using UnityEngine; [CustomEditor (typeof (EditAnimator))] public class EditAnimatorInspector: Editor {/ the current time of the slider / private float masked CurTime; / whether it has been baked / private bool masked HasBake; / whether the current playback status is previewed / private bool m_Playing / current running time / private float masking running time; / Last system time / private double masked PreviousTime; / Total recording time / private float milled RecorderStopTime; / Total slider length / private const float kDuration = 30f Private Animator masked Animator; private EditAnimator editAnimator {get {return target as EditAnimator;}} private Animator animator {get {return m_Animator?? (m_Animator = editAnimator.GetComponent ();}} 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 () } / Bake recording animation data / private void bake () {if (m_HasBake) {return;} if (Application.isPlaying | | animator = = null) {return;} const float frameRate = 30f; const int frameCount = (int) ((kDuration * frameRate) + 2) Animator.Rebind (); animator.StopPlayback (); animator.recorderStartTime = 0; / / start recording specified number of frames animator.StartRecording (frameCount); for (var I = 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; } /// /// 进行预览播放 /// private void play() { if (Application.isPlaying || animator == null) { return; } bake(); m_RunningTime = 0f; m_Playing = true; } /// /// 停止预览播放 /// private void stop() { if (Application.isPlaying || animator == null) { return; } m_Playing = false; m_CurTime = 0f; } /// /// 预览播放状态下的更新 /// private void update() { if (Application.isPlaying || animator == null) { return; } if (m_RunningTime >M_RecorderStopTime) {m_Playing = false; return;} / / set the time position of playback animator.playbackTime = masked running time; animator.Update (0); m_CurTime = m_RunningTime } / private void manualUpdate () {if (animator & &! m_Playing & & m_HasBake & & m_CurTime) is used to play the current animation frame through the slider in the non-preview playback state.
< 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(); } }} 检视器效果如下:Click "Bake" and you can see that the pose of the character becomes the first frame of the animation state, as shown below:
Drag the slider to play the frame at the current time, as follows:
Click "Play" to automatically play the entire animation, as shown below:
Finally, u3dnotes.com warm Tip: through Unity3d's Wwindow- > Animation window, you can easily preview and even edit animation offline in the editor. XD
The above is all the contents of the article "how to control the playback of Animator under the Unity editor". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.