Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Scrollbar steals focus...

Discussion in 'Immediate Mode GUI (IMGUI)' started by podperson, Nov 15, 2007.

  1. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    I have a bunch of GUI code that basically does something like:

    1) If there's more than N things to show, display a scrollbar.

    2) Display up to N things (which N things depends on the scrollbar setting, if there is one)

    3) Display an TextField.

    The interesting thing is that I can add things to the list by typing into the TextField ... and when I reach N+1 things, the TextField loses focus.

    If I display the TextField before I display (or don't display) the scrollbar, the problem goes away.

    Note that displaying a varying number of things doesn't cause the EditField to lose focus; it's the scrollbar being drawn that causes the problem.

    Here's a code fragment:

    Code (csharp):
    1. function OnGUI () {
    2.     if( visible ){
    3.         GUI.skin = hudSkin;
    4.         consoleRect = GUI.Window(0, consoleRect, ConsoleWindow, "Weasel Console v0.2");
    5.     }
    6. }
    7.  
    8. function ConsoleWindow( windowID : int ){
    9.     var textWidth : int = Screen.width - 80;
    10.     var textHeight : int = 28;
    11.    
    12.     while(transcript.length > maxTranscriptLength){
    13.         transcript.Shift();
    14.     }
    15.     while(commandHistory.length > maxCommandHistoryLength){
    16.         commandHistory.Pop();
    17.     }
    18.    
    19.     commandLine = GUI.TextField(Rect(24,204,textWidth - 112, textHeight + 8), commandLine);
    20.    
    21.     if(transcript.length > 5){
    22.         transcriptScroll = GUI.VerticalScrollbar(Rect(textWidth+24, 48, 16, 140), transcriptScroll, 5, transcript.length, 0);
    23.     } else {
    24.         transcriptScroll = 0;
    25.     }
    26.    
    27.     GUI.Label(Rect(24,24,160,28), "Scroll: " + transcriptScroll);
    28.    
    29.     var j : int = 0;
    30.     for(var i = 0; i < 5; i++){
    31.         j = transcript.length - 5 - transcriptScroll + i;
    32.         if( j >= 0  j < transcript.length){
    33.             GUI.Label(Rect(24, 48 + i*textHeight, textWidth, textHeight), transcript[j]);
    34.         }
    35.     }
    36.    
    37.     if(GUI.Button(Rect(textWidth - 80, 198, 120, 48),"Make It So!")){
    38.         if(commandLine != ""){
    39.             Globals.ProcessCommand(commandLine);
    40.             commandLine = "";
    41.         }
    42.     }
    43. }
    I've already moved the EditField above the scrollbar code which has worked around the problem.