Search Unity

How to draw with OnPreviewGUI?

Discussion in 'Scripting' started by Deleted User, Mar 7, 2012.

  1. Deleted User

    Deleted User

    Guest

    I have a custom asset that internally keeps a Mesh object. I have a custom inspector UI that I use to modify the asset and wanted to add a preview so you could see the mesh being generated. But I can't find any examples or help on how I can do this. Any pointers?
     
  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.  
     
  3. AstralProjection

    AstralProjection

    Joined:
    Feb 12, 2013
    Posts:
    11
    Hey Nick. Sometimes I wish a lot of the Unity features were more like XNA. :D
     
  4. startassets

    startassets

    Joined:
    Feb 13, 2017
    Posts:
    11
  5. Skyblade

    Skyblade

    Joined:
    Nov 19, 2013
    Posts:
    77
    UPD: Solved - forgot to call textute.Apply();

    I still cannot give the idea how to draw inside the OnPreviewGUI.

    This code shows only grey preview, while I expect to see green area:
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. /// <summary>
    5. /// Inspector for .SVG assets
    6. /// </summary>
    7. [CustomEditor(typeof(DefaultAsset))]
    8. public class GrapfVizEditor : Editor
    9. {
    10.     public override bool HasPreviewGUI()
    11.     {
    12.         return true;
    13.     }
    14.  
    15.     public override void OnPreviewGUI(Rect r, GUIStyle background)
    16.     {
    17.         var path = AssetDatabase.GetAssetPath(target);
    18.         if (path.EndsWith(".gv"))
    19.         {
    20.             var texture = new Texture2D(1, 1);
    21.             texture.SetPixel(1, 1, Color.green);
    22.             texture.Apply(); // <- missed that part
    23.             GUI.DrawTexture(r, texture, ScaleMode.StretchToFill, false);
    24.         }
    25.     }
    26.  
    27. }
    28.  
     
    Last edited: Mar 21, 2020