• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DaiMangouDev · Mar 29, 2017 at 10:48 PM · cameraeditoreditor-scriptingpreview

Any idea how to use PreviewRenderUtility ?

I was coding and I typed "preview.." and saw PreviewRenderUtility. A public class with function that seem to allows up to now quickly draw our own preview windows in the unity Editor .... But it has not been documented yet so attempts to make it work are hit and miss.

Anyone used this before and got it working ? Could you how me how it is used ?

If I am correct it should allows us to finally do things like this easily .alt text

prev.png (14.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by chingwa · Jul 11, 2017 at 09:46 PM

@DMDev I noticed code for this after viewing this Unite talk here: https://www.youtube.com/watch?v=sd9KotQ2PhA

You can see them talk about asset previews around the 8:00 minute mark. This is the code they use:

PreviewRenderUtility renderUtility = new PreviewRenderUtility();

void OnGUI(){ Rect rect = new Rect(40,200,200,200); renderUtility.BeginPreview(rect, ""); renderUtility.DrawMesh(previewModel.mesh, Vector3.one, Quaternion.identity, previewModel.material,0); renderUtility.EndAndDrawPreview(rect); }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaiMangouDev · Jul 13, 2017 at 02:59 AM 0
Share

this is great! thanks for sharing

avatar image
1

Answer by EJSainz · Jul 22, 2019 at 02:38 PM

Just in case somebody stomps here looking for some info on PreviewRenderUtility, I just gathered the following code from here and there to draw a prefab using it:

     void BeginDraw(Rect r)
     {
         if (prevRenderer == null)
             prevRenderer = new PreviewRenderUtility();
 
         prevRenderer.camera.transform.position = camPosition;
         prevRenderer.camera.transform.LookAt(Vector3.zero, Vector3.up);
         prevRenderer.camera.farClipPlane = 30;
 
         prevRenderer.lights[0].intensity = 0.5f;
         prevRenderer.lights[0].transform.rotation = Quaternion.Euler(30f, 30f, 0f);
         prevRenderer.lights[1].intensity = 0.5f;
 
         prefabTransform.SetTRS(prefabTranslation, Quaternion.Euler(prefabRotation), prefabScale);
 
         prevRenderer.BeginPreview(r, GUIStyle.none);
     }
 
     void EndDraw(Rect r)
     {
         bool fog = RenderSettings.fog;
         Unsupported.SetRenderSettingsUseFogNoDirty(false);
         prevRenderer.camera.Render();
         Unsupported.SetRenderSettingsUseFogNoDirty(fog);
 
         Texture texture = prevRenderer.EndPreview();
         GUI.DrawTexture(r, texture);
     }
 
     public void DrawRenderPreview()
     {
         if (prefab != null)
         {
             MeshFilter[] meshFilters = prefab.GetComponentsInChildren<MeshFilter>();
             for (int i = 0; i < meshFilters.Length; i++)
             {
                 if (meshFilters[i].sharedMesh)
                 {
                     MeshRenderer meshRenderer = meshFilters[i].gameObject.GetComponent<MeshRenderer>();
                     for (int j = 0; j < meshFilters[i].sharedMesh.subMeshCount; j++)
                     if (meshRenderer != null)
                     {
                             prevRenderer.DrawMesh(
                                 meshFilters[i].sharedMesh,
                                 prefabTransform * meshRenderer.transform.localToWorldMatrix,
                                 meshRenderer.sharedMaterials[j],
                                 j);
                     }
                 }
             }
         }
     }
 
     private void OnGUI()
     {
         Rect r = new Rect(0.0f, 0.0f, position.width, position.height);
 
         BeginDraw(r);
         DrawRenderPreview();
         EndDraw(r);
     }
 
 

This code will draw a GameObject referenced in the prefab variable, and it's part of an EditorWindow. I'm sorry I have not enough time to prepare the full working class, but the code is pretty self explanatory, and you can have it working in no time.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom Inspector Preview Buttons 0 Answers

OnInteractivePreviewGUI multiple selection. Works but spams errors. 0 Answers

Change angle of camera in prefab preview 4 Answers

Cannot close Preview Scene 0 Answers

Get default preview for GameObject Editor 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges