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

No GUI.UnfocusControl... what's the workaround?

Discussion in 'Scripting' started by Adam-Buckner, Jan 27, 2011.

  1. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    There is the focus control:
    http://unity3d.com/support/documentation/ScriptReference/GUI.FocusControl.html

    ... but there is no GUI.UnfocusControl.

    But I find that in Editor Windows, the focus can "get stuck". If you select a field:


    and then change what you are looking at, in this case looking at a new item:


    because the field was focused, it doesn't update the data correctly (it still days 17 in the current item field):


    so when the field is manually refocussed by clicking into another field (in this case the search field):


    ... the correct data is displayed.

    This could be solved with a "GUI.UnfocusControl", there is a GUI.UnfocusWindow:
    http://unity3d.com/support/documentation/ScriptReference/GUI.UnfocusWindow.html

    But no "GUI.UnfocusControl".

    I've tried to work-around it by setting a "GUILayout.Label" as a named control and swapping focus to that control using GUI.FocusControl, but it doesn't work. With this code:
    Code (csharp):
    1.  
    2.     GUI.SetNextControlName ("shiftFocus");
    3.     GUILayout.Label ("Inventory Item Editor", EditorStyles.boldLabel);
    4.  
    5.     viewIndex = EditorGUILayout.IntField ("Current Item", viewIndex, GUILayout.ExpandWidth(false));
    ... this seems to skip the GUILayout.Label as a control and sets the EditorGUILayout.IntField as the field to shift focus to. Which I don't want. I was hoping for an invisible control to shift the focus to, but I can't find a way to shift focus to anything other than a usable field... not a great work around.

    Anyone else find a solution or have any suggestions?
     
  2. Spectre9000

    Spectre9000

    Joined:
    Aug 30, 2010
    Posts:
    170
    At the bottom of your GUI, just put GUI.SetNextControlName(""); Then you can just call GUI.FocusControl (""); to unfocus items.

    It works for me so far. Let me know if you have any problems with that.
     
    tinyant likes this.
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Oooh. Clever.

    That works, it does!

    Thanks for the tip.
     
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Hmm... How about a work around for this one:

    If I have focus on a field:


    ... and want to create or move to a new record:


    ... but also want to set the focus:


    Currently I've tried first dropping focus - which works on its own following the suggestion above...

    Then I've tried a number of attempts to refocus on the field, with code like this:
    Code (csharp):
    1.     void OnGUI () {
    2.         //  snip - Irrelevant code in OnGUI()...
    3.         if (GUILayout.Button("Add Item", GUILayout.ExpandWidth(false))) {
    4.             GUI.FocusControl ("clearFocus");
    5.             AddItem();
    6.         }
    7.     }
    8.  
    9.     void AddItem () {
    10.         //  snip - Irrelevant code to add an item
    11.         newItem = true;
    12.     }
    And later in OnGUI():
    Code (csharp):
    1.  
    2.     if (newItem) {
    3.         newItem = false;
    4.         GUI.FocusControl ("Item Name");
    5.     }
    But no matter where I place this in OnGUI, it grabs the previous information and retains it:


    This should say "New Item".

    Bah and Bargle!

    I was hoping by forcing it to drop focus and then refocusing the field, it would find the new piece of information...
     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Hrm...

    A pause/wait for one frame co-routine could probably do the trick. I faked it with:
    Code (csharp):
    1.     void SetNewItemFocus () {
    2.         GUI.FocusControl ("Search Field");
    3.         newItemCount ++;
    4.         if (newItemCount  > 1) {
    5.             newItemCount = 0;
    6.             viewIndex = itemList.Length;
    7.             GUI.FocusControl ("Item Name");
    8.             newItem = false;
    9.         }
    10.     }
    11.  
    I guess the editor window just needs a moment to get it's wits together.
     
  6. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Cheers, this helped a lot!