In my EditorWindow I want a TextArea that grows with its content

I’m using EditorGUILayout.TextArea and its working well so far.

But sometimes there’s only 2 or 3 lines so I want it to only use that.
Is it possible to do some tricks with GUILayout.ExpandHeight(true) or so to make that happen?

I tried all sorts of combinations with .ExpandHeight, .MaxHeight, … but it never works like I want it to.

If I could measure exactly how much space a given text would take (given a max width so it also includes line wrapping!) I could easily make it work by myself. But there’s no such function, is there?

Here’s what I want exactly:

  • If the text in the text area is very short, I want the TextArea to stull consume a given minimum hieght.

  • If the text is too big, it should expand in height until a given maximum height, and then a scrollbar should appear.

  • If the text is somewhere inbetween, it should be exactly as big as it needs to be.

Somehow its possible, Unity already does it!

Have you tried GUIStyle.CalcHeight()?

Yes, in fact I have.
And I feel its the right way, but there’s still something missing (wrapping is still incorrect).

I was able to trace the source of the problem. When I do EditorGUILayout.TextArea alone, everything is fine. However when I am inside an EditorGUILayout.BeginHorizontal(); things get a little messed up.

Here’s what I am doing in order:

  • BeginHorizontal()
  • PrefixLabel()
  • CalcHeight(position.width) + TextArea
  • EndHorizontal()

I figured out that position.width doesn’t give the current width, but the width of the whole window (You gotta admit, it’s named a little bit ambiguous :smile:).

But how would I know where I am in terms of “horizontal progress”, or in other words where the next control will start on the x axis?

I tried some magic with HorizontalScope but somehow that doesn’t get me anywhere…

Begin horizontal returns a Rect, from which you could acquire the width.