Search Unity

GUI Text Field - Remove Focus

Discussion in 'Immediate Mode GUI (IMGUI)' started by dannylam4, Feb 22, 2010.

  1. dannylam4

    dannylam4

    Joined:
    Feb 22, 2010
    Posts:
    5
    So I'm trying to do a FPS kind of thing, and incorporate a "cheat screen" for debugging purposes. Basically, there's just a few check boxes in the top right corner, enabling infinite ammo, and some other conditions, and a text field that controls the user's speed. When I click the text field, I kinda expected that when I click on an area outside the text field, that it would lose focus, but it still sticks. So now, when I try to move with the WASD keys, it's being typed into the text field. Anyone have any ideas how to make the text field lose focus? Thanks!
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Hi, welcome to the forum!

    You might be able to use GUI.FocusControl to switch focus to an inert control or a "dummy" one rendered offscreen.
     
  3. dannylam4

    dannylam4

    Joined:
    Feb 22, 2010
    Posts:
    5
    Ahh, okay... yeah, this looks like it could work, haha. I just went for the show and hide cheat screen, which forces focus to go away when hidden. It does return focus when it unhides, which is kinda annoying, so I'll give this a shot for that. Thanks!
     
  4. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. namespace Com.DankoKozar
    4. {
    5.     public class FocusBuster
    6.     {
    7.         public static string FOCUS_OUT_UID = "_________Copyright 2012 by Danko Kozar";
    8.  
    9.         /// <summary>
    10.         /// Blurs Unity focus
    11.         /// Should be called from inside the OnGUI() handler
    12.         /// </summary>
    13.         public static void BlurFocus()
    14.         {
    15.             GUI.SetNextControlName(FOCUS_OUT_UID);
    16.             GUI.Label(new Rect(-100, -100, 1, 1), "");
    17.             GUI.FocusControl(FOCUS_OUT_UID);
    18.         }
    19.     }
    20. }
     
  5. tasadar

    tasadar

    Joined:
    Nov 23, 2010
    Posts:
    290
    Try this to unfocus control:

     
    Ash-Blue, flashframe and Jean-Fabre like this.
  6. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    I found a bug in my snippet. You have to replace GUI.Label with GUI.TextField to work properly:

    Code (csharp):
    1. using UnityEngine;
    2.      
    3.     namespace Com.DankoKozar
    4.     {
    5.         public class FocusBuster
    6.         {
    7.             public static string FOCUS_OUT_UID = "_________by Danko Kozar 2012";
    8.      
    9.             /// <summary>
    10.             /// Blurs Unity focus
    11.             /// Should be called from inside the OnGUI() handler
    12.             /// </summary>
    13.             public static void BlurFocus()
    14.             {
    15.                 GUI.SetNextControlName(FOCUS_OUT_UID);
    16.                 GUI.TextField(new Rect(-100, -100, 1, 1), "");
    17.                 GUI.FocusControl(FOCUS_OUT_UID);
    18.             }
    19.         }
    20.     }
     
    Last edited: Jun 18, 2012
  7. garkin

    garkin

    Joined:
    Jun 16, 2012
    Posts:
    3
    Remove focus from any TextField to nowhere:
    Code (csharp):
    1. GUIUtility.keyboardControl = 0;
     
    Last edited: Jun 16, 2012
  8. Alexphauge

    Alexphauge

    Joined:
    Jan 25, 2010
    Posts:
    2
    Thanks a bunch for this thread! Saved me from quite the headaches! :)
     
  9. AhrenM

    AhrenM

    Joined:
    Aug 30, 2014
    Posts:
    74
    @garkin

    That line of code should be inscribed in platinum
     
    jvella_bhvr and XGT08 like this.