- Home /
Only activate chat if T is pressed
Hi, I customized a youtube tutorial script for a multiplayer chat. I just need some help with some features with it.
You can only write if T is pressed
You can send messages by pressing enter
Messages gets removed after a certain time
Here's the two codes:
using UnityEngine; using System.Collections;
public class ChatBase : MonoBehaviour { public enum Clean { Full = 0, Semi = 1, none = 0 }; public static Clean cleaning = Clean.Semi; public static ArrayList messages = new ArrayList(); public static string user = "";
public string[] Swares;
private string messageToSend = "";
private void Start()
{
DontDestroyOnLoad(gameObject);
}
public static void CleanChat()
{
ArrayList temp = new ArrayList();
for (int x = 1; x < messages.Count; x++)
{
temp.Add(messages[x]);
}
messages = temp;
}
public void sendMessage(string mess)
{
messageToSend = mess;
if (mess != "")
{
for (int c = 0; c < mess.Length; c++)
{
foreach (string s in Swares)
{
if (mess[c].ToString().ToUpper() == s[0].ToString().ToUpper())
{
string temp = "";
int extra = 0;
for (int x = c; x < messageToSend.Length; x++)
{
if (messageToSend[x].ToString().ToUpper() != messageToSend[0].ToString().ToUpper())
{
if (messageToSend[(x - 1)] == messageToSend[x])
{
extra++;
continue;
}
}
if (messageToSend[x].ToString().ToUpper() != s[s.Length - 1].ToString().ToUpper())
{
temp += messageToSend[x].ToString().ToUpper();
}
else
{
string reg = temp + messageToSend.Substring(x, 1).ToString().ToUpper();
if (reg.ToUpper() == s.ToUpper())
{
string firstHalf = messageToSend.Substring(0, c);
string secondHalf = messageToSend.Substring(c + s.Length + extra, messageToSend.Length - s.Length - c - extra);
string stars = new string('*', s.Length);
messageToSend = firstHalf + stars + secondHalf;
}
break;
}
}
}
}
if (c == (mess.Length - 1))
networkView.RPC("ReciveMessage", RPCMode.All, user + ": " + messageToSend);
}
}
}
[RPC]
private void ReciveMessage(string sentMess)
{
messages.Add(sentMess);
Invoke("Recheck", 5);
}
private void Recheck()
{
if (cleaning == Clean.Semi)
CleanChat();
}
}
using UnityEngine; using System.Collections;
public class Chat : MonoBehaviour { private string message = ""; private Vector2 scrollView = Vector2.zero;
private MultiplayerManager MpManager;
public bool IsTyping = false;
private void Start()
{
ChatBase.user = "";
ChatBase.messages = new ArrayList();
ChatBase.user = MultiplayerManager.instance.PlayerName;
}
private void OnGUI()
{
GUILayout.BeginArea(new Rect(Screen.width - 200, Screen.height - 250, 200 ,200));
scrollView = GUILayout.BeginScrollView(scrollView);
foreach(string c in ChatBase.messages)
{
GUILayout.Label(c);
}
GUILayout.EndArea();
GUILayout.EndScrollView();
scrollView.y++;
if (IsTyping == false & Input.GetButton("Chat"))
{
message = GUI.TextField(new Rect(Screen.width - 200, Screen.height - 25, 150, 25), message);
if (GUI.Button(new Rect(Screen.width - 50, Screen.height - 25, 50, 25), "Send"))
{
ChatBase.cleaning = ChatBase.Clean.none;
GameObject.Find("Chat").GetComponent<ChatBase>().sendMessage(message);
message = "";
}
}
}
}
I would really appreciate help with this.
Format your code, don't ask for features, delete this question... Vottedown