I have a custom asset that internally keeps a Mesh object. I have a custom inspector UI that I use to modify the asset and wanted to add a preview so you could see the mesh being generated. But I can’t find any examples or help on how I can do this. Any pointers?
Adding this post to clarify how Editor.OnPreviewGUI and Editor.OnInteractivePreviewGUI work. Please note that, according to the documentation, inspector previews are limited to the primary editor of persistent objects: GameObjectInspector, MaterialEditor, TextureInspector. This means that it is currently not possible for a component to have its own inspector preview. Here is an example:
using UnityEngine;
using UnityEditor;
public class GameObjectEditorWindow: EditorWindow {
GameObject gameObject;
Editor gameObjectEditor;
[MenuItem("Window/GameObject Editor")]
static void ShowWindow() {
GetWindow<GameObjectEditorWindow>("GameObject Editor");
}
void OnGUI() {
gameObject = (GameObject) EditorGUILayout.ObjectField(gameObject, typeof(GameObject), true);
if (gameObject != null) {
if (gameObjectEditor == null)
gameObjectEditor = Editor.CreateEditor(gameObject);
gameObjectEditor.OnPreviewGUI(GUILayoutUtility.GetRect(500, 500), EditorStyles.whiteLabel);
}
}
}
Hey Nick. Sometimes I wish a lot of the Unity features were more like XNA.
Hi Nick, I’ve just finished my own preview asset. Maybe it will be useful for you: Powerful Preview - Extend your assets with a preview. - Community Showcases - Unity Discussions.
UPD: Solved - forgot to call textute.Apply();
I still cannot give the idea how to draw inside the OnPreviewGUI.
This code shows only grey preview, while I expect to see green area:
using UnityEditor;
using UnityEngine;
/// <summary>
/// Inspector for .SVG assets
/// </summary>
[CustomEditor(typeof(DefaultAsset))]
public class GrapfVizEditor : Editor
{
public override bool HasPreviewGUI()
{
return true;
}
public override void OnPreviewGUI(Rect r, GUIStyle background)
{
var path = AssetDatabase.GetAssetPath(target);
if (path.EndsWith(".gv"))
{
var texture = new Texture2D(1, 1);
texture.SetPixel(1, 1, Color.green);
texture.Apply(); // <- missed that part
GUI.DrawTexture(r, texture, ScaleMode.StretchToFill, false);
}
}
}