- Home /
Why is this chat box sending messages from bottom to top?
Hello! I need help. I have this chatbox found in the Photon Demo but its sending messages from bottom to top.... which.... no chat box in history ever really does... any way i can make it send from top to bottom instead??
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PhotonView))]
public class InRoomChat : Photon.MonoBehaviour
{
public Rect GuiRect = new Rect(0,0, 323,170);
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[i]);
}
GUILayout.EndScrollView();
GUILayout.BeginHorizontal();
GUI.SetNextControlName("ChatInput");
inputLine = GUILayout.TextField(inputLine);
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);
}
}
Comment
$$anonymous$$ight be as easy as changing this line:
for (int i = messages.Count - 1; i >= 0; i--)
{
GUILayout.Label(messages[i]);
}
to this:
for (int i = 0; i < messages.Count; i++)
{
GUILayout.Label(messages[i]);
}
Your answer
Follow this Question
Related Questions
Create objects with script? 2 Answers
How can I tell if my character controller is in contact with a wall? 1 Answer
Multiple people working in the same project 4 Answers
WWW Video with control buttons 0 Answers
Help with Ragdoll 2 Answers