Skip to content

Instantly share code, notes, and snippets.

@CodeSmile-0000011110110111
Last active August 2, 2022 09:51
Show Gist options
  • Save CodeSmile-0000011110110111/a3856d5e62f09cc3f5457ccce2c60146 to your computer and use it in GitHub Desktop.
Save CodeSmile-0000011110110111/a3856d5e62f09cc3f5457ccce2c60146 to your computer and use it in GitHub Desktop.
Unity MeshPreview embedded in UI Toolkit VisualElement using IMGUIContainer
// MonoBehaviour
using UnityEngine;
public class MeshPreview : MonoBehaviour
{
public Mesh mesh;
[Range(1, 1000)] public int previewHeight = 100;
public bool showSettings = true;
}
---------------------------------------------------
// Editor script
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
[CustomEditor(typeof(MeshPreview))]
public class MeshPreviewEditor : Editor
{
private UnityEditor.MeshPreview preview;
private void OnDisable()
{
if (preview != null)
{
preview.Dispose();
preview = null;
}
}
public override void OnInspectorGUI()
{
DrawDefaultInspector();
var mp = target as MeshPreview;
if (preview == null)
preview = new UnityEditor.MeshPreview(mp.mesh);
if (preview.mesh != mp.mesh)
preview.mesh = mp.mesh;
var rect = GUILayoutUtility.GetRect(1, mp.previewHeight);
preview.OnPreviewGUI(rect, "TextField");
if (mp.showSettings)
preview.OnPreviewSettings();
}
public override VisualElement CreateInspectorGUI()
{
var customInspector = new VisualElement();
customInspector.Add(new IMGUIContainer(() => { OnInspectorGUI(); }));
return customInspector;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment