Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

EditorWindow TextArea MinHeight problem - bug?

Discussion in 'Editor & General Support' started by mitchmunro, Jul 2, 2021.

  1. mitchmunro

    mitchmunro

    Joined:
    Jun 20, 2019
    Posts:
    15
    Hello!

    I'm learning Editor Scripting, and I'm trying to make an Editor Window with a Text Area that has a minimum height of about 5 lines, and extends to add more lines if you make more. MinHeight isn't working as expected.

    Here is my code:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public class MyWindow : EditorWindow
    5. {
    6.     private string testText;
    7.  
    8.     [MenuItem("My Tools/My Window")]
    9.     static void Init()
    10.     {
    11.         // Get existing open window or if none, make a new one:
    12.         EditorWindow.GetWindow(typeof(MyWindow));
    13.     }
    14.  
    15.     void OnGUI()
    16.     {
    17.         GUIStyle style = new GUIStyle(EditorStyles.textArea);
    18.         style.wordWrap = true;
    19.  
    20.         GUILayout.Label("Text Area");
    21.  
    22.         testText = EditorGUILayout.TextArea(testText, style, GUILayout.MinHeight(70));
    23.     }
    24. }
    So currently, when
    GUILayout.MinHeight(70)
    is added, the minimum height works well, but the text area size doesn't extend properly. If I remove that bit of code, the Text Area expands as needed, but of course it is only 1 line high.

    Any ideas what I need to change to make it work? Or is it a bug? I couldn't find any other answers after googling for a while! Thanks in advance!
     
  2. Thomas-Bousquet

    Thomas-Bousquet

    Joined:
    Dec 19, 2016
    Posts:
    41
    +1 on this; it really feels like a bug cause by
    style.wordWrap = true;
    - I'll probably submit one later.
    For the moment, I'm using the following workaround, but it's only possible because I know the width of the TextArea
    Code (CSharp):
    1. GUIContent textCT = new GUIContent(text);
    2. float minHeight = 5 * EditorGUIUtility.singleLineHeight;
    3. float textHeight = EditorStyles.textArea.CalcHeight(textCT, textAreaWidth);
    4.  
    5. EditorGUILayout.TextArea(text), GUILayout.Height(Mathf.Max(minHeight, textHeight)), GUILayout.Width(textAreaWidth);
    6.