unity-game-enginetextareaeditorinspector

Why does the textfield not display the proper value, when i select it? (unity custom inspector)


I'm having trouble with my custom unity editor.

I have created a TextArea in my editor script, but it acts strange when i try to change the value via code, while also selecting it.

TextArea

textEditor.text = EditorGUILayout.TextArea(textEditor.text, GUILayout.MinHeight(100));

The yellow buttons at the bottom are set up to add a string of text to the textarea. For example, "bold" adds "<b> </b>". But when I press the buttons, the text does not show up in the TextArea. I know the problem isn't the code that adds the text, as the added text eventually appears in the TextArea, once I deselect the TextArea.

The button calls a function called "AddFormat" like this: textEditor.AddFormat(textEditor.textFormats[i]);. Inside my class is the AddFormat function, that just says text += _format.openingTag + " " + _format.closingTag;

I have tried looking for some way to either automatically deselect the TextArea or redraw the editor all together. I also tried Repaint().


Solution

  • jonas-echterhoff suggests:

    While you are editing a TextArea or TextField, changes to the source string will not be picked up by the text editor. You could try removing Focus from the TextArea when you want to change the text by calling:

    GUIUtility.keyboardControl = 0;
    GUIUtility.hotControl = 0;