Search Unity

Scrollview - How make it scroll up instead of down?

Discussion in 'Immediate Mode GUI (IMGUI)' started by GSZ, Oct 1, 2014.

  1. GSZ

    GSZ

    Joined:
    Dec 13, 2012
    Posts:
    11
    I'm using a scrollview as a chat box in my online game. I have it set so my chat messages appear with the newest message at the bottom of the chat window. All is working well, except I cannot seem to figure out how to make the scrollview scroll with the new messages at the bottom, and allow the player to scroll up to see old messages.

    Instead, the new messages continue to fill the bottom of the window, but the scrollview sits at the top making the new messages not visable to players unless they manually scroll to the bottom to see new messages.

    Is there a way to invert the scroll box so that it scrolls down with the chat by default and a player can manually choose to scroll up to see older messages?

    I'm using the InRoomChat script that comes with PUN. The only change I made was to invert the messages so that the newest appears at the bottom instead of the top. The code is below:

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(PhotonView))]
    6. public class InRoomChat : Photon.MonoBehaviour
    7. {
    8.     public Rect GuiRect = new Rect(0,0, 400,200);
    9.     public bool IsVisible = true;
    10.     public bool AlignBottom = false;
    11.     public List<string> messages = new List<string>();
    12.     private string inputLine = "";
    13.     private Vector2 scrollPos = Vector2.zero;
    14.     //private Vector2 scrollPos = new Vector2(0, 400);
    15.  
    16.  
    17.     public static readonly string ChatRPC = "Chat";
    18.  
    19.     public void Start()
    20.     {
    21.         if (this.AlignBottom)
    22.         {
    23.             this.GuiRect.y = Screen.height - this.GuiRect.height;
    24.         }
    25.     }
    26.  
    27.     public void OnGUI()
    28.     {
    29.         if (!this.IsVisible || PhotonNetwork.connectionStateDetailed != PeerState.Joined)
    30.         {
    31.             return;
    32.         }
    33.        
    34.         if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
    35.         {
    36.             if (!string.IsNullOrEmpty(this.inputLine))
    37.             {
    38.                 this.photonView.RPC("Chat", PhotonTargets.AllBuffered, this.inputLine);
    39.                 this.inputLine = "";
    40.                 GUI.FocusControl("");
    41.                 return; // printing the now modified list would result in an error. to avoid this, we just skip this single frame
    42.             }
    43.             else
    44.             {
    45.                 GUI.FocusControl("ChatInput");
    46.             }
    47.         }
    48.  
    49.         GUI.SetNextControlName("");
    50.         GUILayout.BeginArea(this.GuiRect);
    51.  
    52.         scrollPos = GUILayout.BeginScrollView(scrollPos);
    53.         GUILayout.FlexibleSpace();
    54.         for (int i = 0; i < messages.Count; i++)
    55.         //for (int i = messages.Count - 1; i >= 0; i--)
    56.         {
    57.             GUILayout.Label(messages[i]);
    58.         }
    59.         GUILayout.EndScrollView();
    60.  
    61.         GUILayout.BeginHorizontal();
    62.         GUI.SetNextControlName("ChatInput");
    63.         inputLine = GUILayout.TextField(inputLine);
    64.         if (GUILayout.Button("Send", GUILayout.ExpandWidth(false)))
    65.         {
    66.             this.photonView.RPC("Chat", PhotonTargets.AllBuffered, this.inputLine);
    67.             this.inputLine = "";
    68.             GUI.FocusControl("");
    69.         }
    70.         GUILayout.EndHorizontal();
    71.         GUILayout.EndArea();
    72.     }
    73.  
    74.     [RPC]
    75.     public void Chat(string newLine, PhotonMessageInfo mi)
    76.     {
    77.         string senderName = "anonymous";
    78.  
    79.         if (mi != null && mi.sender != null)
    80.         {
    81.             if (!string.IsNullOrEmpty(mi.sender.name))
    82.             {
    83.                 senderName = mi.sender.name;
    84.             }
    85.             else
    86.             {
    87.                 senderName = "player " + mi.sender.ID;
    88.             }
    89.         }
    90.  
    91.         this.messages.Add(senderName +": " + newLine);
    92.     }
    93.  
    94.     public void AddLine(string newLine)
    95.     {
    96.         this.messages.Add(newLine);
    97.     }
    98. }
    Thanks for any help anybody may be able to provide!
     
  2. aoe_labs

    aoe_labs

    Joined:
    Nov 4, 2013
    Posts:
    42
    Here's what I think will work:

    Since you're using the scrollview with a scrollbar (I'm assuming, since you said the users have to manually scroll), set the scrollbar's value property accordingly. You can find value under the direction property - which you will also need to set - in the scrollbar component.

    You can set the direction to TopToBottom or BottomToTop, depending on what fits your needs. Then change the value property. The value property is the starting value of the scrollbar, namely it's position along the scrollbar.
    For instance, if you have the direction set to TopToBottom and the value is 0, the scrollbar slider will be at the top when the game starts. If it's at 1, it will be at the bottom.