Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to draw with OnPreviewGUI?

Discussion in 'Scripting' started by kellygravelyn, Mar 7, 2012.

  1. kellygravelyn

    kellygravelyn

    Joined:
    Jan 22, 2009
    Posts:
    143
    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