It still seems like default inspector drawing in UI Toolkit is not a functional thing, but I wanted to double check since this has been an issue on the board since mid 2019.
I went ahead and made a minimal window that draws the active selection object’s editor. This seems to work fine. I’m assuming that this bizarre offset in the nested inspector drawn the same way is due to inherited style settings or something in a parent. Does this sound like something that could be happening?
Apparently the old approach I have for drawing this is problematic in 2021.x with Arrays so I need to migrate to InspectorElement asap. This layout issue is the only roadblock but I cannot find any setting to prevent this from happening and the other properties drawn as Elements are laid out just fine (they’re in the exact same place in hierarchy) so I’m at a loss as to why this is happening.
After thinking about it, could it be that the name width is pulled from the EditorWindow itself (wow I hope not)? Why don’t the properties that are manually drawn out inherit in the same way?
Minimal window code that works fine:
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
public class TestWindow : EditorWindow
{
private static TestWindow m_editorWindow;
private static Object m_selection;
[MenuItem("Test/Editor Window", priority = 0)]
public static void Open()
{
if (m_editorWindow != null)
{
FocusWindowIfItsOpen(typeof(TestWindow));
return;
}
m_editorWindow = GetWindow<TestWindow>();
}
public void OnEnable()
{
}
public void OnGUI()
{
if (m_selection == Selection.activeObject) return;
m_selection = Selection.activeObject;
Change();
}
private void Change()
{
rootVisualElement.Clear();
InspectorElement inspector = new InspectorElement(m_selection);
Editor editor = Editor.CreateEditor(m_selection);
inspector.Bind(editor.serializedObject);
rootVisualElement.Add(inspector);
}
}
Snippet of how the other inspector is drawn (broken alignment) :
Okay, I made an isolated case that can repro. The InspectorElement has clear issues drawing properties with the proper width in a layout.
Reported as case 1404093.
In the example I basically make 3 columns and put the InspectorElement into the third column, and it proceeds to fail to layout correctly. If we won’t draw the other two columns, it aligns content fine because it’s aligned with the EditorWindow.
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
public class TestWindow : EditorWindow
{
private static TestWindow m_editorWindow;
private static Object m_selection;
[MenuItem("Test/Editor Window", priority = 0)]
public static void Open()
{
if (m_editorWindow != null)
{
FocusWindowIfItsOpen(typeof(TestWindow));
return;
}
m_editorWindow = GetWindow<TestWindow>();
}
public void OnEnable()
{
rootVisualElement.style.width = 800;
}
public void OnGUI()
{
if (m_selection == Selection.activeObject) return;
m_selection = Selection.activeObject;
Change();
}
private void Change()
{
rootVisualElement.Clear();
rootVisualElement.style.flexDirection = FlexDirection.Row;
// make columns
VisualElement leftElement = new VisualElement();
leftElement.style.minWidth = 200;
leftElement.style.width = 200;
leftElement.Add(new Label("LEFT COLUMN"));
VisualElement centerElement = new VisualElement();
centerElement.style.minWidth = 200;
centerElement.style.width = 200;
centerElement.Add(new Label("CENTER COLUMN"));
VisualElement rightElement = new VisualElement();
rightElement.style.minWidth = 400;
rightElement.style.width = 400;
rightElement.Add(new Label("RIGHT COLUMN"));
// make inspector
InspectorElement inspector = new InspectorElement(m_selection);
Editor editor = Editor.CreateEditor(m_selection);
inspector.Bind(editor.serializedObject);
// insert
rightElement.Add(inspector);
rootVisualElement.Add(leftElement);
rootVisualElement.Add(centerElement);
rootVisualElement.Add(rightElement);
}
}
I can confirm that there are issues using the InspectorElement class. I am attempting to show fields inside of a state graph, and I see unresponsive buttons and overflowing content, no matter what I do with the parent VisualElements