In Unity 2020.1.0a19, with a custom editor I can add text to the bottom of a MonoBehaviour’s default Inspector GUI using:
[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
GUILayout.Label("test imgui");
}
}
However I can’t seem to figure out how to do the same thing using just the new CreateInspectorGUI().
I can override public VisualElement CreateInspectorGUI() but the base just returns null. I can’t seem to get a visual element that is the current inspector, and then append a TextElement to that:
[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
public override VisualElement CreateInspectorGUI()
{
VisualElement gui = base.CreateInspectorGUI(); // returns null
TextElement text = new TextElement();
text.text = "test element";
gui.Add(text); // unsurprising null ref exception
return gui;
}
}
I’m assuming that this is because the Inspector has not been fully ported and so it only falls back to the original OnInspectorGUI() implementation when CreateInspectorGUI() returns null. Is that about right?
Is there any workaround or easy way I can start using VisualElements to extend a default base Inspector without having to manually re-draw each property using the new system?
Goodness… They decided using “HTML” and “CSS” (note the quotes) instead of XAML (note the lack of quotes) wasn’t enough, so they made a basic operation unintuitive.
By the way, DrawDefaultInspector() isn’t even an exact replica. Actually, I have the hypothesis that it is, but your editor overrides parts of the default editor, or something. The custom editor doesn’t work with certain components either, I suppose it’s vice-versa of the above.
Unity, get your shit together: this is .NET, not the web. You make the much needed move of killing UnityScript and then this? Are you trying to apologize to the web developers or what? This is also .NET in 2020, not in the 90s; why the hell do we need an attribute instead of just doing this:
public class MonoBehaviorEditor : Editor<MonoBehavior> // Notice the lack of the "u" in MonoBehaviour. US English.
{
public override bool EditorForChildClasses => true; // Don't worry, I do understand the excruciating pain caused by following established .NET naming conventions.
public override VisualElement CreateInspectorGui() // Another convention: "Gui" instead of "GUI": https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.Uri/src/System/Uri.cs#L260
{
VisualElement visualElement = base.CreateInpectorGui();
visualElement.hierarchy.Insert(0, new HelpBox($"Type: {Target.GetType().Name}", HelpBoxMessageType.Info));
return visualElement;
}
}
Now tell me the current way is better. Backwards compatibility is a problem when it gets in your way, you know. Ask, I don’t know, this guy. There’s just way too much wrong with Unity right now.
EDIT: Okay, I calmed down a little. I figured out that you don’t actually have to override OnInspectorGUI() there, you can just call base: container.Add(new IMGUIContainer(base.OnInspectorGUI)). This doesn’t change my opinion on anything described, though.
[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
public override VisualElement CreateInspectorGUI()
{
var root = new VisualElement();
InspectorElement.FillDefaultInspector( root , this.serializedObject , this );
root.Add(new Label("This is a custom inspector"));
return root;
}
}
prev solution
[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
public override VisualElement CreateInspectorGUI()
{
var root = new VisualElement();
root.Add(new IMGUIContainer(base.OnInspectorGUI));
root.Add(new Label("This is a custom inspector"));
return root;
}
}