unity在scene下动画预览
using UnityEngine; public class EditAnimator : MonoBehaviour { public float m_CurTime; public int selectedAnimationIndex; }
using UnityEditor; using UnityEditor.Animations; using UnityEngine; [CustomEditor(typeof(EditAnimator))] public class EditAnimatorInspector : Editor { private float m_CurTime; private const float kDuration = 1f; private Animator m_Animator; // 定义动画名称的数组 private string[] animationOptions = { }; private EditAnimator editAnimator { get { return target as EditAnimator; } } private Animator animator => m_Animator ?? (m_Animator = editAnimator.GetComponent<Animator>()); public override void OnInspectorGUI() { if (animationOptions.Length == 0) { GetAllAnimation(); } editAnimator.selectedAnimationIndex = EditorGUILayout.Popup("选择动画", editAnimator.selectedAnimationIndex, animationOptions); editAnimator.m_CurTime = EditorGUILayout.Slider("时间:", editAnimator.m_CurTime, 0f, kDuration); string str = animationOptions[editAnimator.selectedAnimationIndex]; if (!string.IsNullOrEmpty(str)) { animator.Play(str, 0, editAnimator.m_CurTime); } animator.Update(0); // 添加按钮 if (GUILayout.Button("刷新动画")) { GetAllAnimation(); } } private void GetAllAnimation() { // 获取 Animator 中的动画片段 if (animator.runtimeAnimatorController is AnimatorController controller) { var clips = controller.animationClips; animationOptions = new string[clips.Length]; for (int i = 0; i < clips.Length; i++) { animationOptions[i] = clips[i].name; } } } }