Chat GUI - Change Chat order?

I’m using Photon’s default chat script, and I’m trying to figure out how to have new messages that are sent go from the bottom UP, instead of appear at the top of the chat box.

Here is the script.

using System.Collections.Generic;
using UnityEngine;
using System.Collections;

[RequireComponent(typeof(PhotonView))]
public class InRoomChat : Photon.MonoBehaviour 
{
    public Rect GuiRect = new Rect(0,0, 250,300);
    public bool IsVisible = true;
    public bool AlignBottom = false;
    public List<string> messages = new List<string>();
    private string inputLine = "";
    private Vector2 scrollPos = Vector2.zero;

    public static readonly string ChatRPC = "Chat";

    public void Start()
    {
        if (this.AlignBottom)
        {
            this.GuiRect.y = Screen.height - this.GuiRect.height;
        }
    }

    public void OnGUI()
    {
        if (!this.IsVisible || PhotonNetwork.connectionStateDetailed != PeerState.Joined)
        {
            return;
        }
        
        if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
        {
            if (!string.IsNullOrEmpty(this.inputLine))
            {
                this.photonView.RPC("Chat", PhotonTargets.All, this.inputLine);
                this.inputLine = "";
                GUI.FocusControl("");
                return; // printing the now modified list would result in an error. to avoid this, we just skip this single frame
            }
            else
            {
                GUI.FocusControl("ChatInput");
            }
        }

        GUI.SetNextControlName("");
        GUILayout.BeginArea(this.GuiRect);

        scrollPos = GUILayout.BeginScrollView(scrollPos);
        GUILayout.FlexibleSpace();
        for (int i = messages.Count - 1; i >= 0; i--)
        {
            GUILayout.Label(messages*);*

}
GUILayout.EndScrollView();

GUILayout.BeginHorizontal();
GUI.SetNextControlName(“ChatInput”);
inputLine = GUILayout.TextField(inputLine);
if (GUILayout.Button(“Send”, GUILayout.ExpandWidth(false)))
{

  •  	if(string.IsNullOrEmpty(this.inputLine))*
    
  •  	{*
    
  •  		return;*
    
  •  	}*
    

this.photonView.RPC(“Chat”, PhotonTargets.All, this.inputLine);
this.inputLine = “”;
GUI.FocusControl(“”);
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
}

[RPC]
public void Chat(string newLine, PhotonMessageInfo mi)
{
string senderName = “anonymous”;

if (mi != null && mi.sender != null)
{
if (!string.IsNullOrEmpty(mi.sender.name))
{
senderName = mi.sender.name;
}
else
{
senderName = "player " + mi.sender.ID;
}
}

this.messages.Add(senderName +": " + newLine);
}

public void AddLine(string newLine)
{
this.messages.Add(newLine);
}
}
I’m not to good with GUI’s, so some help would be great!

Bump, anyone? I'd love some help.

–

1 Answer

1

Change line 52 from

    for (int i = messages.Count - 1; i >= 0; i--)

to

    for (int i = 0; i < messages.Count; i++)

edit
In addition you might need to implement some kind of “autoscrolling” since a ScrollView is used and new lines would appear off-view at the bottom. It might be enough to set

scrollPos.y = 10000000;

inside the Chat method when a new line is added. This should force the ScrollView more or less to the end.

It looks like the chat history doesn’t have a max-line-limit. Even when using a large one you always should implement one :wink:

Something like this at the end of the Chat method:

    while (messages.Count > MAXLINES)
        messages.RemoveAt(0);

Wow, I just got back home, and I see the perfect fixes, you're a great ol' chap. Thank you kindly!

–

this is great but i couldn'go up to check my histrory!!!

–

thanx bunny but i got the solution before it by putting scrollpos = mathf.infinity;

–