Is it valid to instantiate a TextElement?

I’m just learning the new UI Toolkit and I found that I could instantiate a TextElement class.

using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

[CustomEditor(typeof(MeshFilter))]
public class Inspector : Editor
{

    private VisualElement _root;

    public override VisualElement CreateInspectorGUI()
    {
        return _root;
    }


    public void OnEnable()
    {
        _root = new VisualElement();

        TextElement textElement = new TextElement();
        textElement.text = "Some Text";
        _root.Add(textElement);
           
    }

}

Is this expected behaviour?

Yes. Part of UI elements’ draw is to be able do it nearly all programatically if you wish.

1 Like