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. Dismiss Notice

OnPreviewGUI and friends

Discussion in 'Scripting' started by Fenrisul, Jul 13, 2014.

  1. Fenrisul

    Fenrisul

    Joined:
    Jan 2, 2010
    Posts:
    617
    I am completely baffled by how hard it has been to get this working...

    PreviewRenderUtility seems to be the right choice to draw a Mesh inside a CustomPreview but I can't for the life of me figure out how to properly use it within the OnPreviewGUI call...

    Anybody have any working examples? I've already trolled the internet the best I know how.
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    That's a rather vague question... What exactly isn't working?

    And if you want an already pre-made solution for displaying preview, check my signature for Advanced Inspector.
     
  3. Fenrisul

    Fenrisul

    Joined:
    Jan 2, 2010
    Posts:
    617
    Man you are super useful lol (not being sarcastic at all - you know plenty about editor scripting, would love to pick your brain off-forum sometime)


    To be more specific, I would like to draw an arbitrary mesh inside a ObjectPreview's OnInteractivePreviewGUI or OnPreviewGUI call and would like to know if there's a "Preferred" method for doing this rather than just GL'ing my way through it.

    edit:
    Would like to draw the mesh inside the Inspector's Preview window, not a SceneView.
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Sorry... I meant that this is a very large situation. My package's file for handling previews is over 600 lines of code.

    OnPreviewGUI is only a simple GUI drawing, so you need to create a camera, a copy of your mesh, lights, place everything in an empty scene, render it to a texture, and draw that texture on the GUI, most likely by using GUI.DrawTexture.

    If you want some control, like dragging the mesh around, it becomes a bit more complex.

    My advice would be to get a reflection application, like .NET Reflector, and to take a peak at the UnityEditor.dll.
     
  5. Fenrisul

    Fenrisul

    Joined:
    Jan 2, 2010
    Posts:
    617
    Thank you :) That's all I needed to know heh. Didn't want to delve that deep if I didn't have to.
     
  6. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    If you don't need anything fancy and just a view of your mesh "as is", the PreviewRenderUtility can do the job just fine... Just noticed there is no API documentation for this class.

    It should probably look like this;

    Code (csharp):
    1. public override void OnPreviewGUI(Rect region, GUIStyle background)
    2. {
    3.     previewUtility.BeginPreview(region, background);
    4.     previewUtility.DrawMesh(myMesh, Matrix4x4.identity, myMaterial, 0);
    5.     Texture texture = previewUtility.EndPreview();
    6.     GUI.DrawTexture(region, texture, ScaleMode.StretchToFill, false);
    7. }
     
    Novack likes this.
  7. Fenrisul

    Fenrisul

    Joined:
    Jan 2, 2010
    Posts:
    617
    I think my hang up was that I didn't realize preview utility has its own camera of sorts built in.

    I think i'm good now :)