Search Unity

Help with OnPreviewGUI for editor scripting

Discussion in 'Editor & General Support' started by chronosapien, Apr 9, 2013.

  1. chronosapien

    chronosapien

    Joined:
    Feb 1, 2011
    Posts:
    73
    I've searched though tons of Google pages and watched many videos but have not been able to find any information on using OnPreviewGUI or OnInteractivePreviewGUI for editor scripting. In contacting customer support I was sent links to the scripting reference and the tutorial page.

    Has anyone used these in editor scripts before that has some insight they would like to share? I would like to preview prefabs that I create through the script and this seems like the way to do it but as we all know the documentation for editor scripting is basically non existent.
     
  2. unimechanic

    unimechanic

    Joined:
    Jan 9, 2013
    Posts:
    155
    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:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class GameObjectEditorWindow: EditorWindow {
    5.    
    6.     GameObject gameObject;
    7.     Editor gameObjectEditor;
    8.    
    9.     [MenuItem("Window/GameObject Editor")]
    10.     static void ShowWindow() {
    11.         GetWindow<GameObjectEditorWindow>("GameObject Editor");
    12.     }
    13.    
    14.     void OnGUI() {
    15.         gameObject = (GameObject) EditorGUILayout.ObjectField(gameObject, typeof(GameObject), true);
    16.        
    17.         if (gameObject != null) {
    18.             if (gameObjectEditor == null)
    19.                 gameObjectEditor = Editor.CreateEditor(gameObject);
    20.            
    21.             gameObjectEditor.OnPreviewGUI(GUILayoutUtility.GetRect(500, 500), EditorStyles.whiteLabel);
    22.         }
    23.     }
    24. }
    25.