Animate a Preview Window in a Custom Editor

Hey all.

I have a custom editor for creating movesets. I’m trying to play an animation clip on the target of a preview window, but it only every refreshes when I click on the preview. I’ve tried every method possible for rereshing the preview, but nothing so far has worked.

The basic of what I’ve got in my Editor Window.

private void OnGUI()
{
    // Draw some things, omitted for shortness...
    EditorGUILayout.BeginVertical("box");
    DrawPreviewWindow();
    EditorGUILayout.EndVertical();
}
private void DrawPreviewWindow()
{
    gameObject = (GameObject)EditorGUILayout.ObjectField(gameObject, typeof(GameObject), true);

    if (gameObject != null)
    {
        if (gameObjectEditor == null)
            gameObjectEditor = Editor.CreateEditor(gameObject);

        if (clip != null)
        {
            scrubTime = EditorGUILayout.Slider(scrubTime, 0f, clip.length);
        }

        GUIStyle bgColor = new GUIStyle();
        gameObjectEditor.OnPreviewGUI(GUILayoutUtility.GetRect(256, 256), bgColor);
        gameObjectEditor.ReloadPreviewInstances();

    }
}
private void Update()
{
    if (gameObject == null) { return; }

    if (clip != null)
    {
        if (!AnimationMode.InAnimationMode())
        {
            AnimationMode.StartAnimationMode();
        }

        AnimationMode.BeginSampling();
        AnimationMode.SampleAnimationClip(gameObjectEditor.target as GameObject, clip, scrubTime);
        AnimationMode.EndSampling();
    }
}

I’ve been scratching my head all day. Any help would be muchly appreciated!

It may be that your Update part is not running normally.
With your code hint I implemented a monobehavior that can provide animation preview.

public class AnimationInitTime : MonoBehaviour
    {
        public float beginTime;

        private Animation m_Animation;
        private string m_DefaultClipName;

        private void Awake()
        {
            m_Animation = GetComponent<Animation>();
            m_DefaultClipName = m_Animation.clip.name;
            m_Animation[m_DefaultClipName].normalizedTime = beginTime;
        }

#if UNITY_EDITOR

       
[CustomEditor(typeof(AnimationInitTime))]
        private class AnimationInitTimeEditor : Editor
        {
            private Animation m_Animation;
            //Avoid conflict with animation editor
            private bool m_IsEditing;

            private void Awake()
            {
                if (target is AnimationInitTime obj)
                {
                    m_Animation = obj.GetComponent<Animation>();
                }
            }

            public override void OnInspectorGUI()
            {
                base.DrawDefaultInspector();

                EditorGUI.BeginChangeCheck();
                GUILayout.Label(m_IsEditing.ToString());
                m_IsEditing = GUILayout.Toggle(m_IsEditing, "Preview", GUI.skin.button, GUILayout.Height(31));
                if (EditorGUI.EndChangeCheck())
                {
                    if (m_IsEditing)
                    {
                        AnimationMode.StartAnimationMode();
                    }
                    else
                    {
                        AnimationMode.StopAnimationMode();
                    }
                }
            }

            public void OnSceneGUI()
            {
                if (!m_IsEditing) return;
                if (target is AnimationInitTime obj)
                {
                    if (!EditorApplication.isPlaying && AnimationMode.InAnimationMode())
                    {
                        AnimationMode.BeginSampling();
                        AnimationMode.SampleAnimationClip(obj.gameObject, m_Animation.clip, obj.beginTime);
                        AnimationMode.EndSampling();
                        SceneView.RepaintAll();
                    }
                }
            }
        }

#endif
    }