In order to use the EditorWithPreview you must derive your own version of it. When you override the abstract functions of this class you will be defining what the rendering component is, and how to configure it with the asset and preview material.
TargetType | A ScriptableObject derived class |
Two things are required of your derived class: First, the TargetType class must be a "savable" asset (e.g SciptableObject derived class). So, when the asset is selected from the project view, this editor is shown as the object's inspector. Second, there must exist a component that is capable of rendering this asset to a scene. It is also required that this render component uses an assignable material, or use GL functions to do the rendering. For example, a Mesh is a savable asset, and the MeshRenderer component draws this asset to the scene.
The "Preview" shader provided with this module is required for this class to work properly. You may also choose to create your own preview shader (selectable in PreviewEditor Preferences), which MUST be marked with the following tag:
Furthermore, the preview shader ignores existing scene lights, and instead draws white light in a direction specified by shader parameters:
and
Public Member Functions | |
void | GenerateSnapshot (Quaternion rotation, Vector2 size, ref RenderTexture snapShot) |
This function is the "magic" part of the class. It will manipulate the diorama objects, based on the parameters, and then render it to a RenderTexture. | |
void | GenerateSnapshot (Quaternion rotation, Vector2 size, ref Texture2D snapShot) |
This Public function allows the user to generate the Snapshot, and get the result in the form of a Texture2D. | |
abstract override string | GetInfoString () |
This function returns the string that will be displayed as the title of the preview pane in the editor. Your derived class will need to define this abstract function. | |
override bool | HasPreviewGUI () |
Specifies that this type of editor window should indeed draw a preview pane. | |
virtual void | OnEnable () |
When enabled will perform setup steps required to create the diorama, and prepares to render it. | |
override void | OnInspectorGUI () |
Override this function if you would like to change how the inspector of the editor is drawn. This version just draws the Default Inspector. | |
override void | OnInteractivePreviewGUI (Rect rect, GUIStyle background) |
Calls the OnPreviewGUI function since the same logic is used to draw both. | |
override void | OnPreviewGUI (Rect rect, GUIStyle background) |
This function will render the diorama and display it in the Rect provided. PreviewPref options will adjust how the preview is rendered. If the rect is below 32 pixels in height, the cashed thumbImage will be drawing the provided Rect, rather than re-rendering the entire diorama. | |
override Texture2D | RenderStaticPreview (string assetPath, Object[] subAssets, int width, int height) |
Overrides the Unity Editor class function of the same name to provide static Previews of objects. Not intended for use by end User. | |
void | RepaintNow () |
Call this function to force the preview to be repainted. | |
override bool | RequiresConstantRepaint () |
Overridden Editor function that informs unity when the editor needs to be repainted. | |
Static Public Member Functions | |
static void | ProjectItemOnGUI (string guid, Rect rect) |
finds or generates icons for items, if they have a EditorWithPreview defined for the type of object. | |
Protected Member Functions | |
virtual void | CreatePreviewObjects () |
Creates all the objects needed to render the diorama including the camera, preview objects, and diorama shoe-box. Protected, but otherwise internal function that you can override for additional creation steps, if needed. | |
virtual void | DestroyPreviewObjects () |
Destroys all the dynamic objects needed to render the diorama like the preview object, and render Textures. Non-changing items like the diorama shoe-box, and preview camera are not destroyed. Protected, but otherwise internal function that you can override for additional destruction steps, if needed. | |
abstract void | DoGLrendering (Camera cam) |
This function is called immediately after the camera is rendered. Put stuff drawn in OnRenderObject in an overridden version of this function, like GL drawing. | |
void | DrawAxis (Camera cam, Vector3 offset) |
Draws an Axis based upon PreviewPrefs settings to the provided camera. | |
abstract Bounds | GetBounds () |
This function will allow the preview camera to be properly placed, such that it encompass the bounds of the object. The preference-option to offset object to bounds center, will affect weather the camera focuses on the center on the returned bounds, or the origin,. | |
abstract System.Type[] | GetTypesOfRendererComponents () |
The function will provide a list of all the components that a preview object will need in order to be rendered to a camera. These components will be automatically created as part of the preview object, in the scene's preview Diorama Shoe-box. This abstract function will need to be defined in your derived class. | |
virtual void | OnDestroy () |
Destroys the dynamic elements of the diorama. | |
virtual void | OnDisable () |
Destroys the dynamic elements of the diorama, and resets the rotation of the preview. | |
abstract void | PreCameraRendering (Camera cam) |
This function is called immediately before the preview camera is rendered. Override and declare your own version to call drawing functions that queue up specific render tasks, like Graphics.DrawMesh. | |
abstract void | SetupPreviewObject () |
Performs the setup necessary for the PreviewObject to be drawn to the preview camera. This would include actions like assigning the current Editor's target to the appropriate PreviewObject components. previewObject.GetComponent(MeshFilter>().sharedMesh = (Mesh)target;This abstract function will need to be defined in your derived class. | |
abstract void | SetupPreviewObjectRendererWithMaterial (Material previewMaterial) |
Performs the setup necessary for the PreviewObject to be drawn to the preview camera. This abstract function should setup the current PreviewObject "render" components, with the provided shader. | |
Protected Attributes | |
RenderTexture | previewImage |
A reference to the render texture that is displayed in the preview pane. This image is updated dynamically. The texture is destroyed when the Editor window goes out of scope. The texture is destroyed and recreated, every time the preview is repainted. | |
GameObject | previewObject |
This is a reference to the object created in the scene diorama. It is destroyed when the Editor window goes out of scope. | |
Texture2D | thumbImage |
This image is generated once, is small, and static. It is used to for the Target's icon in both inspector and project window. | |
Properties | |
static Material | axisMaterial [get, set] |
Reference to the material that will be used to draw the coordinate axes, in the preview window. | |
static Camera | previewCamera [get] |
this is the preview camera that will be used for all previews. | |
static Material | previewMaterial [get] |
Reference to the dynamically created preview material, which uses the previwShader | |
static Shader | previewShader [get, set] |
this is the special preview shader used to draw preview objects. | |