Ok so im currently trying to use a gui.textarea and have it display a message that u type(which works only one time), but i run into a problem were when u enter a second message it loops the first message like crazy. Is there a different gui i should b using to make this more simplistic becasue i also want to be able to scroll through the log. This is going to be a MMO so we need chat functionailty soon since we are releasing alpha next week. If u would like to check my code here it is.
using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
[RequireComponent (typeof(GUITexture))]
public class ChatWindowPlacement : MonoBehaviour {
public GUIStyle style;
private GUITexture Image;
private int Screen_width;
private int Screen_height;
public string stringToEdit = "";
public TextMesh speechBubble;
private ArrayList list;
private string msglist = "";
public bool chat = false;
private string path = @"C:\Users\Fa7\Desktop\chatLog.txt";//change for server side
bool userHasHitReturn = false;
void Start () {
list = new ArrayList();
Screen_width = Screen.width;
Screen_height = Screen.height;
Image = guiTexture;
Image.pixelInset = new Rect (Screen_width / 100, Screen_height / 100, Image.pixelInset.width, Image.pixelInset.height);
chatUpdate();
}
// Update is called once per frame
void Update () {
}
void chatUpdate()
{
}
void OnGUI() {
GUIStyle boxStyle = "box";
stringToEdit = GUI.TextField(new Rect(Screen.width /60f,Screen.height / 1.05f, 300,20),stringToEdit,25);
Event e = Event.current;
if(e.keyCode == KeyCode.Return)
{
speechBubble.text = stringToEdit;
chat = true;
boxStyle.wordWrap = true;
using (StreamReader sr = File.OpenText(path))
{
string s = "";
Debug.Log(s);
Debug.Log(list);
while ((s = sr.ReadLine()) != null)
{
list.Add(s);
}
foreach(string msg in list)
{
msglist += msg + "\n";
}
}
}
else if(chat)
{
updater();
}
GUI.TextArea(new Rect(10,10,100,100),msglist,200);
if(GUI.Button( new Rect(Image.pixelInset.x + Image.pixelInset.width / 1.1f, Screen_height - Image.pixelInset.y - Image.pixelInset.height + Image.pixelInset.height/ 20, Image.pixelInset.width / 15, Image.pixelInset.height / 10) , "", style) ){
Debug.Log("Closing Chat");
gameObject.SetActive(false);
}
}
void updater()
{
if(chat)
{
if(File.Exists(path))
{
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(stringToEdit);
sw.Close();
chat = false;
}
}
}
}
}