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]
}
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.
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);
}
}
}