Search Unity

Editor GUI inputfield loses focus when GUI updates

Discussion in 'Scripting' started by spvn, Jul 25, 2018.

  1. spvn

    spvn

    Joined:
    Dec 10, 2013
    Posts:
    80
    I'm currently creating a custom editor extension.

    If the user is editing a text field in an Editor window and the GUI updates (e.g. displaying an error message above the text box in the form of a labelfield), the text field will lose focus. Is there any way to prevent the inputfield from losing focus so the user can carry on typing?
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    Hmm... I never experienced that problem.
    Maybe you can post the relevant code, so we can try...
     
  3. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    338
    Might be a bit hacky and not work at all, but you can try the input onDeselect or onEndEdit to put the focus back into the input field.
    Of course just as a last resort, as the best thing to do would be to stop making it lose focus when you don't want it it.
    You can also consider doing a silent error message if the inputfield is currently being used. Check the event system current selected.
     
  4. spvn

    spvn

    Joined:
    Dec 10, 2013
    Posts:
    80
    There isn't a snippet of code I can copy over, but the basic idea is something like this. This is contained within OnGUI() of an EditorWindow class.

    Code (CSharp):
    1. void OnGUI() {
    2.  
    3. [INDENT]bool invalidTextValue = false;
    4.  
    5. string text = EditorGUILayout.TextField(text);
    6.  
    7. if (invalidTextValue) {
    8.  
    9.         EditorGUILayout.LabelField(errorMsg);
    10. }[/INDENT]
    11. }
    if
    invalidTextValue
    becomes true WHILE the user is typing in the textfield, the labelfield will show up and the keyboard will lose focus from the textfield. Is there anyway to prevent this from happening?
     
  5. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    how does the value become true? where is the logic for it?

    by the way:
    string text = EditorGUILayout.TextField(text);
    will not work. You are declaring
    text
    inside the
    OnGUI()
    Method so it will be always created newly and the text field will always be empty. I wonder if that actually compiles since you declare and use the text variable in the same statement... I guess your real code looks different, because this shouldn't be possible.
     
  6. villevli

    villevli

    Joined:
    Jan 19, 2016
    Posts:
    89
    If you currently have the label before the text field, try placing it after it. The control id of the text field may change if you conditionally call the EditorGUILayout.LabelField before calling EditorGUILayout.TextField, and this will cause GUIUtility.keyboardControl to no longer point to the text field, meaning that the keyboard focus is lost. A better approach would be to just change the error message to empty or change the size of the label field to zero when there is no error.