Search Unity

GUILayout.BeginScrollView's scroll bar does not go down automatically.

Discussion in 'Scripting' started by leegod, Sep 14, 2012.

  1. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    why? and how can I move scrollbar to the bottom (to show most recent message) automatically when new message posted and scroll bar generated?

    Thanks.
     
  2. avidesk

    avidesk

    Joined:
    Sep 12, 2011
    Posts:
    62
    You need to modify the scrollPosition variable. It is a Vector2. I don't know what your code looks like, but I am assuming that you are adding a new label to your list of messages. When you get a new message you would add the height of that label to the 'y' property of the scrollPosition variable. You only want to call this when you get a message. If you have it called on Update, it will continue to repeat and make the ScrollView unusable.

    Code (csharp):
    1.  
    2. void NewMessageReceived()
    3. {
    4. scrollPosition = new Vector2(scrollPosition.x, scrollPosition.y + messageLabelHeight);
    5. }
    6.  
    7.  
     
  3. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    @avidesk Thanks for reply. Yes I am using vector2.zero and label for list of messages. It was in the unity official reference. But I remember, when I use this method in unityscript, it automatically scroll to down when new message generated, but I now writing in C#, it doesn't.
     
  4. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    anyway, your method solved problem. I added one line in OnGUI.

    for(int a = 0; a<entries.Count; a++){
    GUILayout.Label(entries[a]);
    NewMessageReceived();
    }

    This can automatically catch what multiple line of messages generated, and adjust scrollbar down to it.