- Home /
Animation Preview in Custom Asset Inspector Window
Hey!
I am making an editor extension and I want to have a box that has a preview of an animated mesh in it.
I got it kind of working, but not completely so I feel really close but can't quite get it!
This displays the GameObject and I can rotate around it by clicking and dragging. The game object does not animate:
GameObject previewGameObject;
AnimationClip animationClip;
Editor myEditor;
float sampleTime = 0.5f;
void OnInspectorGUI()
{
if(myEditor == null) myEditor = Editor.CreateEditor(previewGameObject);
if(myEditor.target != previewGameObject)
{
Editor.DestroyImmediate(myEditor);
myEditor = Editor.CreateEditor(previewGameObject);
}
AnimationMode.StartAnimationMode();
AnimationMode.BeginSampling();
AnimationMode.SampleAnimationClip(previewGameObject, animationClip, sampleTime);
myEditor.DrawPreview(new Rect(0, 0, 300, 300));
AnimationMode.EndSampling();
AnimationMode.StopAnimationMode();
}
To get the animation to work I have to assign the editor every frame, which is dumb and also stops me from being able to rotate around the object:
GameObject previewGameObject;
AnimationClip animationClip;
Editor myEditor;
float sampleTime = 0.5f;
void OnInspectorGUI()
{
Editor.DestroyImmediate(myEditor);
myEditor = Editor.CreateEditor(previewGameObject);
AnimationMode.StartAnimationMode();
AnimationMode.BeginSampling();
AnimationMode.SampleAnimationClip(previewGameObject, animationClip, sampleTime);
myEditor.DrawPreview(new Rect(0, 0, 300, 300));
AnimationMode.EndSampling();
AnimationMode.StopAnimationMode();
}
I have tried a bunch of things, but nothing works as I want to so far!
I hope someone out there has experience in this because this animation preview is quite important to my extension.
Thanks! :)
Hey, did u get any solution for the problem. i am also running into same problem.
Answer by startassets · Mar 12, 2017 at 07:46 PM
@Alismuffin I've just finished my own preview asset, maybe it will be useful for you: https://forum.unity3d.com/threads/powerful-preview.460157/
Answer by Squeazer · May 17, 2021 at 11:30 AM
If anyone else is going through the pain of trying to do this, and comes across this answer, here is how it can be done:
if (clipEditor == null) {
clipEditor = UnityEditor.Editor.CreateEditor(clip);
clipEditor.HasPreviewGUI();
}
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
clipEditor.OnPreviewSettings();
GUILayout.EndHorizontal();
clipEditor.OnInteractivePreviewGUI(GUILayoutUtility.GetRect(256, 256), EditorStyles.whiteLabel);
You have to call HasPreviewGUI because it initializes some internal stuff (AvatarPreview), for some reason. Calling OnPreviewSettings is optional and draws the buttons above the preview.