Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Set focus to a GUI object

Discussion in 'Immediate Mode GUI (IMGUI)' started by Apache, May 26, 2009.

  1. Apache

    Apache

    Joined:
    Sep 26, 2008
    Posts:
    134
    Hi guys, I need to set the keyboard focus to a TextArea (GUILayout) when my GUI Window will be shown for the first time.
    I mean that I want to get the default focus to the textarea but if I click on a different GUI component I need to focus on the clicked component.

    If this is possible I'm also wondering if there are differences between giving the focus to a GUI.TextArea or a GUILayout.TextArea component.


    THANKS !!!!
     
  2. Apache

    Apache

    Joined:
    Sep 26, 2008
    Posts:
    134
    Ok, I almost did it but I have some problems.
    Take a look t the following code:
    Code (csharp):
    1.     // the dimension of window
    2.     private Rect windowRect = new Rect (x, y, width, height);
    3.     // the variable to control where the scrollview 'looks' into its child elements
    4.     private Vector2 scrollPosition = new Vector2 (0, 0);
    5.     private bool firstTime; // the flag used to know if we need to give the focus to the TextArea
    6.     private string content; // the TextArea content
    7.    
    8.     /// <summary>
    9.     /// The property used to set and get the enabling status of the GUI
    10.     /// </summary>
    11.     public bool Enabled {
    12.         get {
    13.             return this.enabled;
    14.         } // get
    15.         set {
    16.             this.enabled = value;
    17.             if (value) {
    18.                 firstTime = true;
    19.             } // if
    20.         } // set
    21.     } // Enabled
    22.  
    23.     void OnGUI () {
    24.         // if the GUI is not enabled we disable the component so we don't execute the update
    25.         if (enabled) {
    26.            
    27.             /* all others operations to do*/
    28.            
    29.             // draw the popup window
    30.             windowRect = GUILayout.Window(3, new Rect (x, y, width, height), fillWindow, "Window to Draw", (GUILayoutOption[])null);
    31.  
    32.             /* all others operations to do */
    33.            
    34.             // give the focus to the text area only if this is the first time that GUI is opened
    35.             if (firstTime) {
    36.                 // first of all give the focus to the window
    37.                 GUI.FocusWindow (3);
    38.                 // now give the focus to the drawn textarea
    39.                 GUI.FocusControl ("textarea");
    40.                 // now tell the window that no more focus is required to the textarea
    41.                 firstTime = false;
    42.             } // if
    43.         } // if
    44.     } // OnGUI
    45.    
    46.     private void fillWindow (int windowID) {
    47.        
    48.         /* all others GUI components */
    49.        
    50.         // the label for text area
    51.         GUILayout.Label ("Text Area");
    52.         // we are going to create the text area inside a scroll view to make possible to scroll the content if necessary
    53.         scrollPosition = GUILayout.BeginScrollView (scrollPosition, GUILayout.Height (160));
    54.             // give a name to the next GUI object to create
    55.             GUI.SetNextControlName ("textarea");
    56.             // create the text area
    57.             content = GUILayout.TextArea (content, GUILayout.ExpandHeight (true));
    58.         GUILayout.EndScrollView ();
    59.        
    60.         /* all others GUI components */
    61.  
    62.     } // fillWindow
    The Enabled property is called from extern to enable/disable the behaviour because if I don't need to show the GUI ingame I skip the Update and OnGUI execution.

    However, when I enable the GUI from extern, I want that only the first time the keyboard focus goes to the textarea and if I click on others components of the GUI (e.g. a textfield) I want to be able to operate on them.

    The problem is that the very first time in absolute that I enable the GUI, the focus is not placed on the textarea and on no others component. Then, if I disable and re-enable the GUI again all works properly and this will appen also in the following disables and enables tries.

    What could the problem be? I'm really not able to figure out what I'm mistaking.
     
  3. Apache

    Apache

    Joined:
    Sep 26, 2008
    Posts:
    134
    Ok, I solved also this issue: it seems that the OnGUI need more cycles to draw the GUI so, on my machine, if I wait 4 cycles (2 frame) on the fifth one I can set the focus on the control.
    Here's an example:
    Code (csharp):
    1.    // the dimension of window
    2.    private Rect windowRect = new Rect (x, y, width, height);
    3.    // the variable to control where the scrollview 'looks' into its child elements
    4.    private Vector2 scrollPosition = new Vector2 (0, 0);
    5.    private bool firstTime; // the flag used to know if we need to give the focus to the TextArea
    6.    private string content; // the TextArea content
    7.    private int veryFirstTimeCounter; // the counter used to know when to apply the focus on the TextArea
    8.    
    9.    /// <summary>
    10.    /// The property used to set and get the enabling status of the GUI
    11.    /// </summary>
    12.    public bool Enabled {
    13.       get {
    14.          return this.enabled;
    15.       } // get
    16.       set {
    17.          this.firstTime = value;
    18.          this.enabled = value;
    19.       } // set
    20.    } // Enabled
    21.  
    22.     void Start () {
    23.         // set the very first time counter to the value needed to wait
    24.         veryFirstTimeCounter = 5;
    25.     } // Start
    26.  
    27.    void OnGUI () {
    28.       // if the GUI is not enabled we disable the component so we don't execute the update
    29.       if (enabled) {
    30.          
    31.          /* all others operations to do*/
    32.          
    33.          // draw the popup window
    34.             windowRect = GUILayout.Window(3, new Rect (x, y, width, height), fillWindow, "Window to Draw", (GUILayoutOption[])null);
    35.  
    36.          /* all others operations to do */
    37.          
    38.    } // OnGUI
    39.    
    40.    private void fillWindow (int windowID) {
    41.      
    42.       /* all others GUI components */
    43.      
    44.       // the label for text area
    45.         GUILayout.Label ("Text Area");
    46.       // we are going to create the text area inside a scroll view to make possible to scroll the content if necessary
    47.       scrollPosition = GUILayout.BeginScrollView (scrollPosition, GUILayout.Height (160));
    48.          // give a name to the next GUI object to create
    49.          GUI.SetNextControlName ("textarea");
    50.          // create the text area
    51.          content = GUILayout.TextArea (content, GUILayout.ExpandHeight (true));
    52.       GUILayout.EndScrollView ();
    53.      
    54.       /* all others GUI components */
    55.  
    56.         // give the focus to the text area only if this is the first time that report gui is open
    57.         if (firstTime) {
    58.             // if this is the very first time the counter is different to 1
    59.             if (veryFirstTimeCounter == 1) {
    60.                 // first of all give the focus to the report window
    61.                 GUI.FocusWindow (3);
    62.                 // now give the focus to the drawn textarea
    63.                 GUI.FocusControl ("textarea");
    64.                 // now tell the window that no more focus is required to the textarea
    65.                 firstTime = false;
    66.             } else {
    67.                 veryFirstTimeCounter--;
    68.             } // if-else
    69.         } // if
    70.  
    71.    } // fillWindow
    I hope that is clear enough; if not feel free to ask all you want !!! :wink: