Search Unity

Reading the extent of a scrollbar in a scroll view

Discussion in 'Immediate Mode GUI (IMGUI)' started by DocSWAB, Dec 4, 2007.

  1. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    We have a chat window with text that's added to the bottom of the chat transcript when the user types into a text field.

    The transcript text string is in a GUILayout.BeginScrollView, and displays properly with the scrollbar using a scrollPosition variable.

    What I'd like to do is every time a new line is added and the scrollbar updates, read the new bottom limit of the scrollbar and set the scrollPosition to that value to force it to scroll down and show the bottom of the text transcript all the time.

    Where/how is that value exposed?

    Thanks in advance!
    Steve
     
  2. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Here's how I generate scrollbars for my command line system (same basic issue you're dealing with):

    transcriptScroll = GUI.VerticalScrollbar(Rect(textWidth+24, 48, 16, 140), transcriptScroll, 5, transcript.length, 0);

    You'll note that the scrollbar takes as inputs the size of the scrollbar, and the current position. You control all this, so it's pretty straightforward to scroll to the end or maintain the existing offset.

    In this case, when a line gets added to the transcript, I increment transcriptScroll.
     
  3. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    Pod, I should have thought of the (somewhat) brute force but obvious method of adding the height of a line of text. I guess I was avoiding that since I have a hard time telling exactly how high a line is, but I'm sure I can measure it.

    EDIT: This worked perfectly for GUILayout as well (scroll position is vector2):

    Code (csharp):
    1.  
    2. scrollPosition.y += transcriptLineHeight;
    3. scrollPosition = BeginScrollView(scrollPosition);
    4.  
    Thanks again,
    Steve
     
  4. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    If you want to scroll to the extremes, you can just assign a ridiculously high or low value - they will get clamped to whatever the GUILayout figures out is the extent
     
  5. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    Thanks, I wondered about that...