Last active
August 2, 2022 09:51
-
-
Save CodeSmile-0000011110110111/a3856d5e62f09cc3f5457ccce2c60146 to your computer and use it in GitHub Desktop.
Unity MeshPreview embedded in UI Toolkit VisualElement using IMGUIContainer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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