[SOLVED] Replicating GUILayout.TextArea

As an exercise I tried converting one of my custom inspectors to use UIElements.

In my original code I have a TextArea that uses a string field local to the inspector class.

string json;

public override void OnInspectorGUI()
{
    json = GUILayout.TextArea(json);

    if (GUILayout.Button("Do something"))
    {
        // do something with json string
    }
}

I had to use IMGUIContainer for this bit as I just don’t know how to get the TextArea working with UIElements. I tried using a TextField but when I click in it it just goes blank. Do I need to somehow bind it to the string field?

Ok, solved it.

I simply needed to register a callback for value changed.

            var textField = new TextField();
            textField.RegisterValueChangedCallback((e) => {
                json = textField.value = e.newValue;
            });

Just a note that you don’t need to re-set the textField.value in your RegisterValueChangedCallback(). textField.value = something is actually what triggers a ChangeEvent and the only reason this is not an infinite loop is that we do an equality check before we send the event.

1 Like