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

[EditorGUI]Preview renderer

Discussion in 'Immediate Mode GUI (IMGUI)' started by MODev, Jul 5, 2014.

  1. MODev

    MODev

    Joined:
    Jul 30, 2013
    Posts:
    229
    Hello,
    have you ever used PreviewRenderUtility? I'm asking because I would like to render mesh in editor window in some defined Rect. But I cannot do that(dunno why, I don't see anything),. Can you give me advice/example how can I render that?
     
  2. icepick912

    icepick912

    Joined:
    Jul 8, 2010
    Posts:
    58
    This is how I draw meshes to an editor window.

    Code (CSharp):
    1. private PreviewRenderUtility mPrevRender;
    2.         private Mesh mPreviewMesh;
    3.         private Material mMat;
    4.  
    5.         public void DrawRenderPreview(Rect r)
    6.         {
    7.             if (mPrevRender == null)
    8.                 mPrevRender = new PreviewRenderUtility();
    9.  
    10.             if (mPreviewMesh == null)
    11.             {
    12.                 mPreviewMesh = Resources.Load("SquareTest", typeof(Mesh)) as Mesh;
    13.                 mMat = Resources.Load("Materials/Box01Mat", typeof(Material)) as Material;
    14.             }
    15.  
    16.             mPrevRender.m_Camera.transform.position = (Vector3)(-Vector3.forward * 8f);
    17.             mPrevRender.m_Camera.transform.rotation = Quaternion.identity;
    18.             mPrevRender.m_Camera.farClipPlane = 30;
    19.  
    20.             mPrevRender.m_Light[0].intensity = 0.5f;
    21.             mPrevRender.m_Light[0].transform.rotation = Quaternion.Euler(30f, 30f, 0f);
    22.             mPrevRender.m_Light[1].intensity = 0.5f;
    23.  
    24.             mPrevRender.BeginPreview(r, GUIStyle.none);
    25.             mPrevRender.DrawMesh(mPreviewMesh, -Vector3.up * 0.5f, Quaternion.Euler(-30f, 0f, 0f) * Quaternion.Euler(0f, 60, 0f), mMat, 0);
    26.  
    27.             bool fog = RenderSettings.fog;
    28.             Unsupported.SetRenderSettingsUseFogNoDirty(false);
    29.             mPrevRender.m_Camera.Render();
    30.             Unsupported.SetRenderSettingsUseFogNoDirty(fog);
    31.             Texture texture = mPrevRender.EndPreview();
    32.  
    33.             GUI.DrawTexture(r, texture);
    34.         }
     
    CodeSmile, Novack and Vlad-Moldovanov like this.
  3. MODev

    MODev

    Joined:
    Jul 30, 2013
    Posts:
    229
    Thanks:)
     
  4. Novack

    Novack

    Joined:
    Oct 28, 2009
    Posts:
    841
    This post is old, but I can't find the documentation on PreviewRenderUtility class. Any idea where to find it?
     
  5. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    385
    chadiik and Novack like this.
  6. CyberFox01

    CyberFox01

    Joined:
    Oct 5, 2012
    Posts:
    23