Any examples of CustomPreviewAttribute, or otherwise creating a custom preview in the editor?

The documentation on CustomPreviewAttribute is kind of weak. Anyone have an example of creating a preview in the editor? Thanks!

Yes, I kinda found out how it works. It works the same way every custom (inspector, attribute…) works. Create a class that inherits from ObjectPreview and uses the CustomPreviewAttribute.

using UnityEngine;
using UnityEditor;

[CustomPreview(typeof(Rigidbody2D))]
public class TestPreview : ObjectPreview {

    public override bool HasPreviewGUI ()
    {
        return true;
    }

    public override void OnPreviewGUI (Rect r, GUIStyle background)
    {
        Debug.Log ("Test");
    }
}

This class will enable the preview area of the inspector if the GameObject has a Rigidbody2D component attached even though this wouldn’t be the case normally. With every change of that preview area, the Log-Text “Test” is fired.

1 Like