Search Unity

Notice to devs: 3 Bugs discovered in TextArea & TextField

Discussion in 'Editor & General Support' started by createdbyx, Feb 7, 2016.

  1. createdbyx

    createdbyx

    Joined:
    May 24, 2012
    Posts:
    39
    Writing to inform I have found 3 BUGS in GUI.TextArea & GUILayout.TextField method overloads.

    The crazy thing that has me wondering is how long these bugs have been in there. In all this time has no one else filed a bug report on this? Seems like they should have been discovered by now.

    Filed bug report: https://fogbugz.unity3d.com/default.asp?768436_vikdrmh7ernh03ls
    Created Gist: https://gist.github.com/createdbyx/0be5bef4268afb57c429
    Tweeted: @Unity3D https://twitter.com/createdbyx/status/696153794644541440
    Using unity 5.3. but bug probably been there for a while.

    Why am I posting? Selfish & altruistic reasons. But mostly selfish as I need this working for my UIControls GUI Lib.

    I am developing a custom abstracted GUI system (Client Code <--> My Custom GUI Lib <--> Unity Immediate Mode GUI/XNA/XAML etc) and came across a scenario where calling specific methods GUI.TextArea & GUILayout.TextField would not work as expected.

    I frequently use Telerik's JustDecompile and used it to inspect the code for those method overloads and discovered that the multiline argument is not being set to the proper value. See attached screenshot and code snippet provided down below.



    Code (CSharp):
    1. namespace Codefarts.Tests
    2. {
    3.     using UnityEditor;
    4.  
    5.     using UnityEngine;
    6.  
    7.     public class textareatextwin : EditorWindow
    8.     {
    9.         private string text = string.Empty;
    10.  
    11.         public void OnGUI()
    12.         {
    13.             var style = GUI.skin.textArea;
    14.  
    15.             // Calling GUILayout.TextField like this allows it to behave like a TextArea
    16.             this.text = GUILayout.TextField(this.text, 10, GUI.skin.textField, GUILayout.ExpandWidth(true));
    17.  
    18.             // this line fails because the underlying code calls a hidden GUI.DoTextField method that accepts a miltiline argument
    19.             // but the multiline argument is set to false which causes the GUI.TextArea to behave like a GUI.TextField
    20.             // and you are not able to press the return key to start a new line      
    21.             this.text = GUI.TextArea(new Rect(0, 40, 100, 100), this.text, 10, style);
    22.  
    23.             // not specifying a style works fine but in my case I needed only one text area to use a custom font (Courier New) with a limited
    24.             // number of characters.
    25.             this.text = GUI.TextArea(new Rect(110, 40, 100, 100), this.text, 10);
    26.         }
    27.  
    28.         /*
    29.         // ===========GUI ============
    30.         public static string TextArea(Rect position, string text, int maxLength, GUIStyle style)
    31.         {
    32.             GUIContent gUIContent = GUIContent.Temp(text);
    33.             GUI.DoTextField(position, GUIUtility.GetControlID(FocusType.Keyboard, position), gUIContent, false, maxLength, style);
    34.             return gUIContent.text;
    35.         }
    36.  
    37.         private static string TextArea(Rect position, GUIContent content, int maxLength, GUIStyle style)
    38.         {
    39.             GUIContent gUIContent = GUIContent.Temp(content.text, content.image);
    40.             GUI.DoTextField(position, GUIUtility.GetControlID(FocusType.Keyboard, position), gUIContent, false, maxLength, style);
    41.             return gUIContent.text;
    42.         }
    43.  
    44.             // ========= GUILayout ==============
    45.         public static string TextField(string text, int maxLength, GUIStyle style, params GUILayoutOption[] options)
    46.         {
    47.             return GUILayout.DoTextField(text, maxLength, true, style, options);
    48.         }
    49.          */
    50.  
    51.         [MenuItem("Test/Test Text Area Style Window")]
    52.         public static void Display()
    53.         {
    54.             GetWindow<textareatextwin>().Show();
    55.         }
    56.     }
    57. }
    58.