Search Unity

How can I use the "Mesh Inspector" preview in an Editor Window?

Discussion in 'Immediate Mode GUI (IMGUI)' started by StewVanB, Jan 18, 2019.

  1. StewVanB


    Apr 12, 2011
    I have built a tool to allow our artists to search for Mesh assets by complexity. This is so they can quickly find assess and correct models in the project. I have also written an "Auto Mesh Simplify" function that I would like to show a preview of before applying the changes to the Mesh asset.

    I can not find any documentation on how Unity renders the preview of the mesh in the Mesh Inspector. I have messed with the PreviewRenderUtility with which I can ultimately generate a 2d image of a specific angle of the model. This works for some objects and not for others as the zoom and framing of the models is largely depending on size. Another caveat of this method is that it doesn't allow for manipulation of the view. It is also missing the Mesh statistical information.

    I want to create an instance of this UI in my own EditorWindow:

    I have thought I could use the GUI loop to add controls to the images and update the image every frame. This seems like a poor solution to what should be an easy implementation of an existing Editor system.

    Thanks for reading my post, any help is appreciated.
    Last edited: Jan 18, 2019
  2. alexanderameye


    Nov 27, 2013
    Hey, I have no idea if this still works or if this did work, but I remember that I managed to get it to work and someone asked me how I did it and I send him this code.

    Code (CSharp):
    1. public class RockStudio : EditorWindow
    2. {
    3.     GameObject gameObject;
    4.     Editor gameObjectEditor;
    5.     Texture2D previewBackgroundTexture;
    6.     void OnGUI ()
    7.     {
    8.         EditorGUI.BeginChangeCheck();
    10.         gameObject = (GameObject) EditorGUILayout.ObjectField(gameObject, typeof(GameObject), true);
    12.         if(EditorGUI.EndChangeCheck())
    13.         {
    14.             if(gameObjectEditor != null) DestroyImmediate(gameObjectEditor);
    15.         }
    17.         GUIStyle bgColor = new GUIStyle();
    19.         bgColor.normal.background = previewBackgroundTexture;
    21.         if (gameObject != null)
    22.         {
    23.             if (gameObjectEditor == null)
    25.             gameObjectEditor = Editor.CreateEditor(gameObject);
    26.             gameObjectEditor.OnInteractivePreviewGUI(GUILayoutUtility.GetRect (200,200),bgColor);
    27.         }
    28.     }
    29. }
  3. alexanderameye


    Nov 27, 2013
    I also have an open source prefab painter on GitHub where I use this interactive preview. You can check it out there how I did it.
    The mesh statistical information is just a label rendered on top of the preview.

    brownboot67 likes this.