I wish to show all component content in a customized editor window, than I have:
EditorWindow creates VisualElement tree containing InspectorElement.
InspectorElement needs to render my component.
My component field with customized property drawer, which is implemented in UIElement style and works fine in normal unity inspector, shows “no GUI implemented”.
ith UI Toolkit Debugger I found IMGUI container in InspectorElement, so I think it’s IMGUI which does not support nesting a VisualElement. So is there any workaround recently? Or latest version of InspectorElement has just implemented without IMGUI?
(Unity Version 2022.2.10, should be new enough…)
Perhaps you can try UnityEditor.CreateEditor
, and get the return on CreateInspectorGUI
?
You can wrap any IMgui in IMGuiContainer in uitoolkit like this
var edtr = UnityEditor.Editor.CreateEditor(gameObject);
GUIStyle bgColor = new GUIStyle();
bgColor.normal.background = EditorGUIUtility.whiteTexture;
var rect = new Rect(prevWindow.contentRect.x, prevWindow.contentRect.y, prevWindow.contentRect.width * 2, prevWindow.contentRect.height);
var t = new IMGUIContainer(() => edtr.OnInteractivePreviewGUI(GUILayoutUtility.GetRect(prevWindow.contentRect.width, prevWindow.contentRect.height), bgColor));
t.style.backgroundColor = Color.white;
Copy pasted from mine, so you should clean it up a bit
I tried this:
var objects = inspectTarget.targetObjects;
var normalEditor = UnityEditor.Editor.CreateEditor(objects);
UnityEngine.Debug.Log(normalEditor);
var element = normalEditor.CreateInspectorGUI();
return element;
but doesn’t work. CreateInspectorGUI
method returns null on any GenericInspector
object, as well as on TransformInspector
.
Edit: After a bit reading in InspectorElement source, It seems that this is the reason why InspectorElement uses a IMGUI container…
After executing this:
var objects = inspectTarget.targetObjects;
var edtr = UnityEditor.Editor.CreateEditor(objects);
GUIStyle bgColor = new GUIStyle();
bgColor.normal.background = EditorGUIUtility.whiteTexture;
var t = new IMGUIContainer(() => edtr.OnInteractivePreviewGUI(GUILayoutUtility.GetRect(400, 800), bgColor));
t.style.backgroundColor = new Color(0.4f, 0.4f, 0.4f, 1f);
the final structure I can discover in UIToolkit Debugger is still a IMGUI containter.
I can see nothing on the white panel and I don’t think it will work properly with CustomPropertyDrawer implemented by CreatePropertyGUI
…
Found a solution with some code digging…
var rt = new ScrollView();
var editor = UnityEditor.Editor.CreateEditor(inspectTarget.targetObjects);
if(editor.GetType().Name != "GenericInspector")
{
var element = editor.CreateInspectorGUI();
if(element != null)
{
rt.AddChild(element);
}
else
{
rt.AddChild(new IMGUIContainer(() => editor.OnInspectorGUI()));
}
}
else
{
InspectorElement.FillDefaultInspector(rt, inspectTarget, editor);
}
rt.Bind(inspectTarget);
return rt;
However I still don’t understand why CreateInspectorGUI
will return null…