Unable to get OnPreviewGUI to fire

Hi All,

I’m having major problems overriding OnPreviewGUI. It seems that OnPreviewGUI isn’t actually firing. Google and this forum has served up no results on all my queries, has anyone gotten this to work?

Here’s what I’m doing in my editor code:

public override bool HasPreviewGUI()
{

[INDENT]return true;[/INDENT]

}

public override void OnPreviewGUI (Rect r, GUIStyle background)
{

[INDENT]Debug.Log("I am working yay!");

Texture myTexture;

// Random code that gets the texture I want to display...

GUI.Box(r, myTexture);
[/INDENT]
}

Still having no luck getting OnPreviewGUI to work. Has anyone got any code that show’s this working?

I’m trying to make it work too. I can make it render, but it hides everything else from the inspector.
It’s not really an onPreviewGUI because I call it from the OnInspectorGUI().

Here’s my sample code:

        public void OnInspectorGUI()
        {
               OnPreviewGUI(new Rect(0,0,300,300), new GUIStyle());
	}
	
	public override void OnPreviewGUI (Rect r, GUIStyle background)
	{
		EditorGUIUtility.RenderGameViewCameras(r,r,false, false);
		//base.OnPreviewGUI(r, background);
	}

This is actually an error in the documentation. The previews are intentionally designed to work only with persistent object editors for the time being, but the docs don’t mention that. They have already been updated but the full release isn’t ready yet so you won’t see the changes immediately.

Are you able to shed some light on “persistent object editors” and what can be done with them?

I’m interested as well!

What I am trying to do is render the preview of an AudioClip into a custom EditorWindow…

I’d like to hear more on this. I’m trying to make a 3D preview for a custom ScriptableObject which holds a “style” for something.

Adding this post to clarify how Editor.OnPreviewGUI and Editor.OnInteractivePreviewGUI work. Please note that, according to the documentation, inspector previews are limited to the primary editor of persistent objects: GameObjectInspector, MaterialEditor, TextureInspector. This means that it is currently not possible for a component to have its own inspector preview. Here is an example:

using UnityEngine;
using UnityEditor;

public class GameObjectEditorWindow: EditorWindow {
	
	GameObject gameObject;
	Editor gameObjectEditor;
	
	[MenuItem("Window/GameObject Editor")]
	static void ShowWindow() {
		GetWindow<GameObjectEditorWindow>("GameObject Editor");	
	}
	
	void OnGUI() {
		gameObject = (GameObject) EditorGUILayout.ObjectField(gameObject, typeof(GameObject), true);
		
		if (gameObject != null) {
			if (gameObjectEditor == null)
				gameObjectEditor = Editor.CreateEditor(gameObject);
			
			gameObjectEditor.OnPreviewGUI(GUILayoutUtility.GetRect(500, 500), EditorStyles.whiteLabel);
		}
	}
}

Hello, i am trying to make a very simple Mecanim Event System, but this code gives me an error.

I am getting also the same error on OnInteractivePreviewGUI, however it works like expected.

DrawGUITexture: texture is null
UnityEditor.AnimationClipEditor:OnInteractivePreviewGUI(Rect, GUIStyle)
MecanimEventEditor:OnGUI() (at Assets/Mecanim Event/Scripts/Editor/MecanimEventEditor.cs:77)
UnityEditor.DockArea:OnGUI()

GUILayout.BeginArea (new Rect (0,100,propertiesArea.width,editor.position.height-100), "Preview", "window");
		scroll=GUILayout.BeginScrollView(scroll);
		gameObject = (Object) EditorGUILayout.ObjectField(gameObject, typeof(Object), true);

		if (gameObject != null) {
			if (gameObjectEditor == null) {
				gameObjectEditor = Editor.CreateEditor (gameObject);
			}
			
		//gameObjectEditor.HasPreviewGUI();
			gameObjectEditor.OnInspectorGUI();
			gameObjectEditor.DrawDefaultInspector();

            gameObjectEditor.OnPreviewSettings();
		//gameObjectEditor.OnPreviewGUI (GUILayoutUtility.GetRect (propertiesArea.width, editor.position.height - 122),EditorStyles.label);//, EditorStyles.whiteLabel);
			gameObjectEditor.OnInteractivePreviewGUI(GUILayoutUtility.GetRect (propertiesArea.width-50, editor.position.height - 122),EditorStyles.whiteLabel);
		//gameObjectEditor.Repaint();
		}
		GUILayout.EndScrollView();
		GUILayout.EndArea ();

Well i experimented a little bit and changing EditorStyles.whiteLabel to another style like “window” removes the error.