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

Deciding how GUILayout.label is drawn on the screen..

Discussion in 'Immediate Mode GUI (IMGUI)' started by DaleNation, Jul 9, 2014.

  1. DaleNation

    DaleNation

    Joined:
    Jul 6, 2014
    Posts:
    30
    Hello,

    In my game I have a chat system. There is an array of the messages that have been sent. Each one is drawn below the one previously sent. It keeps doing that until it reaches the bottom of the scrollable box in which it is contained. When that happens, all the messages scroll up enough to allow the next message to be drawn in at the bottom.

    Is it possible to make it so that the message is *always* drawn at the bottom? And *every* time there is a new message all of the other messages scroll up to make room for the new one?

    Here's the basic code:

    Code (CSharp):
    1.         //Show scroll list of chat messages
    2.         scrollPos = GUILayout.BeginScrollView(scrollPos);
    3.         GUI.color = Color.black;
    4.         for (int i = messages.Count - 1; i >= 0; i--)
    5.         {
    6.             GUILayout.Label(messages[i]);
    7.         }
    8.         GUILayout.EndScrollView();
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    The quick solution is to set scrollPos.y = 10000 (or any value guaranteed to be higher than the inner rect size) as described here.

    You can also base it on the actual size of the content, as in this or this. They use GUI.BeginScrollView (instead of GUILayout.BeginScrollView), but the principle is the same.

    Credits: Google.
     
  3. DaleNation

    DaleNation

    Joined:
    Jul 6, 2014
    Posts:
    30
    Well, that's not necessarily what I meant.

    What I mean is that GUILayout.label writes text from top to bottom, so the array of messages is written like this:

    ===============
    a
    b
    c
    d
    ...
    ===============

    With "a" being the most recent message. What I want is for it to be written like this:

    ===============
    ...
    d
    c
    b
    a
    ===============
     
  4. DaleNation

    DaleNation

    Joined:
    Jul 6, 2014
    Posts:
    30
    Ah! Nevermind! Ahahaha I'm such a fool. Instead of figuring out a way for Unity to draw the messages from bottom to top, I'll just draw them from top to bottom but reverse the loop!