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

How do I make my scroll view scroll to the bottom?

Discussion in 'Immediate Mode GUI (IMGUI)' started by tinman, Aug 5, 2012.

  1. tinman

    tinman

    Joined:
    Jan 8, 2011
    Posts:
    229
    HI Everyone,
    I'm trying to have a chat in my game, and I want the newest message to be on the bottom of the message area(the label). So I need my scroll bar to scroll down with each new message. I'm trying to play around with GUI.ScrollTo but I can't get it working. Here is the code, which is called in OnGUI(). I am trying to change the 'scrollAmount' variable with each message but I can't get it working.
    Please help, thank you!

    Code (csharp):
    1.  
    2.     void ChatWindow(int windowID)
    3.     {
    4.     scrollPosition = GUI.BeginScrollView(viewRect, scrollPosition, new Rect(10, 20, viewRectWidth-20, viewRectHeight+40), false, false );
    5. //  GUI.ScrollTo(new Rect(0,scrollAmount,0,0)); //commented out for now
    6.     GUILayout.Label(message, comStyle);  //Put in the label with automatic layout
    7.     GUI.EndScrollView();
    8.     }
    9.  
    10.  
    11.  
     
  2. SeruK_

    SeruK_

    Joined:
    Jul 27, 2012
    Posts:
    2
    Before

    Code (csharp):
    1. scrollPosition = GUI.BeginScrollView(viewRect, scrollPosition, new Rect(10, 20, viewRectWidth-20, viewRectHeight+40), false, false );
    set scrollPosition to the height of your inner rect (in this case, viewRectHeight+40). Like such:

    Code (csharp):
    1.  
    2. scrollPosition = Vector2(0, viewRectHeight+40);
    3. scrollPosition = GUI.BeginScrollView(viewRect, scrollPosition, new Rect(10, 20, viewRectWidth-20, viewRectHeight+40), false, false );
    4.  
    But first you should check if there's actually a new message somehow. Here's my code for doing something similar (uncommented and a bit messy, it's a debug logger so I don't care about performance):

    Code (csharp):
    1.  
    2. import Debug;
    3.  
    4. private var log:Array = new Array();
    5. var maxLogMessages:int = 200;
    6. var visible:boolean = true;
    7.  
    8. function Start()
    9. {
    10.     log.Add("DebugLogger v.0.1");
    11. }
    12.  
    13. function print(string:String)
    14. {
    15.     log.push(string);
    16.     if(log.length > maxLogMessages)
    17.         log.RemoveAt(0);
    18. }
    19.  
    20. private var scrollPos:Vector2 = Vector2(0, 0);
    21. private var lastLogLen:int = 0;
    22. var printGUIStyle:GUIStyle;
    23. var maxLogLabelHeight:float = 100.0f;
    24.  
    25. function OnGUI()
    26. {
    27.     if(visible)
    28.     {
    29.         var logBoxWidth:float = 180.0;
    30.         var logBoxHeights:float[] = new float[log.length];
    31.        
    32.                 // calculate full height of scrollview
    33.         var totalHeight:float = 0.0;
    34.         var i:int = 0;
    35.         for(var string:String in log)
    36.         {
    37.             var logBoxHeight = Mathf.Min(maxLogLabelHeight, printGUIStyle.CalcHeight(GUIContent(string), logBoxWidth));
    38.             logBoxHeights[i++] = logBoxHeight;
    39.                    
    40.             totalHeight += logBoxHeight+10.0;
    41.         }
    42.    
    43.         var innerScrollHeight:float = totalHeight;
    44.        
    45.                 // if there's a new message, automatically scroll to bottom
    46.         if(lastLogLen != log.length)
    47.         {
    48.             scrollPos = Vector2(0.0, innerScrollHeight);
    49.             lastLogLen = log.length;
    50.         }
    51.        
    52.         scrollPos = GUI.BeginScrollView(Rect(0.0, Screen.height-150.0-50.0, 200, 150), scrollPos, Rect(0.0, 0.0, 180, innerScrollHeight));
    53.        
    54.         var currY:float = 0.0;
    55.         i = 0;
    56.         for(var string:String in log)
    57.         {
    58.             logBoxHeight = logBoxHeights[i++];
    59.             GUI.Label(Rect(10, currY, logBoxWidth, logBoxHeight), string, printGUIStyle);
    60.             currY += logBoxHeight+10.0;
    61.         }
    62.         GUI.EndScrollView();
    63.     } else
    64.     {
    65.         if(GUI.Button(Rect(0,Screen.height-40, 40, 40), "Log"))
    66.             visible = true;
    67.     }
    68. }
    69.  
    EDIT: Cleaned out some irrelevant stuff (were in a rush yesterday).
     
    Last edited: Aug 17, 2012
  3. tinman

    tinman

    Joined:
    Jan 8, 2011
    Posts:
    229
    Thanks a lot! :)
     
  4. diggerjohn

    diggerjohn

    Joined:
    Apr 28, 2009
    Posts:
    60
    Sweet!!!!
    Man have I burned some cycles to discover this.
    Thank you ... 2 years ago .. thanks .....:cool:
     
  5. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    652
    Thanks in advanced!!!
     
  6. WiseOldHooter

    WiseOldHooter

    Joined:
    Jun 16, 2018
    Posts:
    1
    Your the best... AROUND! :D