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

EditorGUILayout.DelayedTextField does not validate when clicking on some other controls

Discussion in 'Editor & General Support' started by Florian-Nouviale, Jun 15, 2021.

  1. Florian-Nouviale

    Florian-Nouviale

    Joined:
    Mar 14, 2013
    Posts:
    51
    Hi,
    I'm facing an issue with DelayedTextField which does not seem to behave as it should.
    I expect that DelayedTextField validates the input text when clicking on any other control but that does not seem to be the case.

    Here is a test example :

    Code (CSharp):
    1.  
    2. using UnityEditor;
    3.  
    4. using UnityEngine;
    5.  
    6. public class DelayedTextFieldTest : EditorWindow
    7. {
    8.  
    9.     string text;
    10.  
    11.     int index;
    12.  
    13.     [MenuItem("TEST/DelayedTextField")]
    14.     private static void OpenWindow()
    15.     {
    16.         OpenWindow(true);
    17.     }
    18.  
    19.     private static DelayedTextFieldTest OpenWindow(bool forceNewWindow)
    20.     {
    21.         DelayedTextFieldTest window;
    22.         if (forceNewWindow)
    23.             window = CreateInstance<DelayedTextFieldTest>();
    24.         else
    25.             window = GetWindow<DelayedTextFieldTest>();
    26.         window.titleContent = new GUIContent("DelayedTextFielTest");
    27.         window.Show();
    28.         return window;
    29.     }
    30.  
    31.     private void OnGUI()
    32.     {
    33.         text = EditorGUILayout.DelayedTextField(text);
    34.         if (GUILayout.Button("Test", GUILayout.Width(60)))
    35.         {
    36.  
    37.             // Open a new window to show that the DelayedTextField is not validated while focus should be lost
    38.             //SubWindow findTypeWindow = ScriptableObject.CreateInstance<SubWindow>();
    39.             //findTypeWindow.Show();
    40.  
    41.             // This is needed to solve the issue
    42.             //GUI.FocusControl("");
    43.         }
    44.  
    45.         // DelayedTextField is not validated when clicking on popup
    46.         index = EditorGUILayout.Popup(index, new string[] { "1", "2", "3" });
    47.         // DelayedTextField is not validated when clicking on popup
    48.         index = EditorGUI.Popup(new Rect(0, 100, 200, 20), index, new string[] { "1", "2", "3" });
    49.     }
    50. }
    51.  
    52.  
    53. public class SubWindow : EditorWindow
    54. {
    55.  
    56. }
    57.  
    As anyone any idea to force DelayedTextField to validate when using controls such as popup ?
     
    Novack likes this.
  2. TechnostalgicGames

    TechnostalgicGames

    Joined:
    Oct 22, 2016
    Posts:
    2
    Any luck figuring this out so far? I am also having this issue. The reason this seems to be happening is I think because GUI.Button is not a selectable control, but it would be great if there was a way to force validate the delayed field.

    Closest thing I have found so far is using
    GUI.FocusControl(null)
    , but unfortunately that doesn't force validation immediately, it only validates on the next tick.
     
    Novack likes this.
  3. Florian-Nouviale

    Florian-Nouviale

    Joined:
    Mar 14, 2013
    Posts:
    51
    I did multiple things (but mainly GUI.FocusControl("");):

    if (resetFocus && Event.current.type == EventType.Repaint)
    {
    resetFocus = false;
    GUI.FocusControl(null);
    EditorGUI.FocusTextInControl(null);
    GUIUtility.keyboardControl = 0;
    }

    I enable resetFocus when saving a custom file, in an editorcouritine to ensure delayedfields are correctly updated before saving
     
    Novack likes this.
  4. Novack

    Novack

    Joined:
    Oct 28, 2009
    Posts:
    840
    Hello, any of you guys would care to elaborate on the workarounds used? Im having exactly the same issue, but with a button instead of a popup. Tried several approaches (including GUI.FocusControl(null)) to no avail.

    The DelayedTextField wont process the input even when is supossedly missing the focus. It works if I click ENTER, and then click on the button, but if I write anything, and go click on the button without hitting enter, the field wont send the input.

    Edit: Nevermind I was too tired :)
    For the record this is how it works for me:
    Code (CSharp):
    1. inputValue= EditorGUI.DelayedTextField(postion, label, inputValue);
    2.  
    3. if (GUI.Button(btnPosition, btnLabel))
    4. {
    5.     resetFocus = true;
    6.     GUI.FocusControl(null);
    7. }
    8.  
    9. if (resetFocus && Event.current.type == EventType.Repaint)
    10. {
    11.     Debug.Log(inputValue);  // 'inputValue' will be properly set.
    12.     resetFocus = false;
    13. }
     
    Last edited: Aug 7, 2022