Skip to content

PreviewRenderUtility

CyberFox Hax edited this page Feb 2, 2021 · 33 revisions

class in UnityEditor Source

Description

Used to render Assets to GUI or to Texture. It can be considered an advanced version of AssetPreview.GetAssetPreview(GameObject). Providing a virtual scene you arrange and render objects in.

Remarks

It's important to call PreviewRenderUtility.Cleanup() is essential to keep it working for long periods of time. The instances will accumulate across compilations and after you hit 64 instances the utility will stop working and you have to restart the editor to continue. (You will get "Preview scene could not be created" errors and the object is never initialized). You only ever need to use 1 instance.

You need to clean up your GameObjects by yourself if you plan on doing multiple renders.

Example

Draws a cube to a 128x128 RenderTexture and displays it OnGUI upscaled to 300x300

using UnityEditor;
using UnityEngine;

public class PreviewRenderUtilityExample : EditorWindow
{
    [MenuItem("Window/PreviewRenderUtilityExample")]
    private static void CreateWindow() {
        var win = GetWindow<PreviewRenderUtilityExample>();
        win.Show();
    }

    private PreviewRenderUtility _previewRenderUtility;
    private Texture _outputTexture;

    private void OnEnable() {
        if (_previewRenderUtility != null)
            _previewRenderUtility.Cleanup();
        _previewRenderUtility = new PreviewRenderUtility(true);
        System.GC.SuppressFinalize(_previewRenderUtility);
        var camera = _previewRenderUtility.camera;
        camera.fieldOfView = 30f;
        camera.nearClipPlane = 0.3f;
        camera.farClipPlane = 1000;
        camera.transform.position = new Vector3(2.5f, 1f, -2.5f);
        camera.transform.LookAt(Vector3.zero);

        var obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
        _outputTexture = CreatePreviewTexture(obj);
        DestroyImmediate(obj);
    }

    private RenderTexture CreatePreviewTexture(GameObject obj) {
        _previewRenderUtility.BeginPreview(new Rect(0, 0, 128, 128), GUIStyle.none);

        _previewRenderUtility.lights[0].transform.localEulerAngles = new Vector3(30, 30, 0);
        _previewRenderUtility.lights[0].intensity = 2;
        _previewRenderUtility.AddSingleGO(obj);
        _previewRenderUtility.camera.Render();

        return (RenderTexture)_previewRenderUtility.EndPreview();
    }

    private void OnGUI() {
        if (_previewRenderUtility != null || _outputTexture != null)
            GUI.DrawTexture(new Rect(0,0, 300, 300), _outputTexture);
    }

    private void OnDisable() {
        _previewRenderUtility.Cleanup();
    }
}

Properties

Name Description
cameraFieldOfView Shortcut for camera.fieldOfView
camera Camera of the virtual scene
ambientColor Ambient color of the virtual scene
lights Read only collection of lights in the virtual scene

Constructors

Name Description
PreviewRenderUtility Creates an instance of the class

Public Methods

Name Description
AddSingleGO Adds a GameObject to the virtual scene
BeginPreviewHDR TBA
BeginPreview Begin rendering context, with style for background
BeginPreviewHDR TBA
BeginStaticPreview Begins rendering context to output a static Texture2D
BeginStaticPreviewHDR TBA
Cleanup Clean up the Utility. Call this OnDisable or suffer the consequences
EndAndDrawPreview Ends the rendering and draws the image on Rect
EndPreview Ends the rendering context and returns the RenderTexture
EndStaticPreview Ends the rendering context and returns the Texture2D
GetScaleFactor TBA
InstantiatePrefabInScene Instanite the GameObject and return the instance
Render Camera.Render() with extras
DrawMesh TBA

Static Methods

Name Description
CreateLight Creates a GameObject with a light component attached