Its very symple, what it does is search if the msg to send contains an @, if so it will treat it as a Pvt msg instead of a public, then it will search for the user with the written name. i atached an empty script called "privateChat" to each user, so i can easily search them. if it finds it it will send a Private call to the server including the msg and player name
This part changes the msg color to green if pvt:
- Code: Select all
foreach(ChatEntryLobby entry in entries) {
GUILayout.BeginHorizontal();
if(!entry.mine && !entry.priv) {
GUILayout.Label(entry.text);
}
if(entry.mine && !entry.priv) {
GUI.contentColor = Color.yellow;
GUILayout.Label(entry.text);
GUI.contentColor = Color.white;
}
if(entry.mine && entry.priv){
GUI.contentColor = Color.green;
GUILayout.Label(entry.text);
GUI.contentColor = Color.white;
}
GUILayout.EndHorizontal();
GUILayout.Space(3);
}
// End the scrollview we began above.
GUILayout.EndScrollView();
and here we search for the username:
- Code: Select all
if(Event.current.type == EventType.keyDown && Event.current.keyCode == KeyCode.Return && inputField.Length > 0) {
if(inputField.Contains ("@")){
PrivateMsg = true;
}else{
PrivateMsg = false;
}
if(!PrivateMsg){
ChatText("You sayd: " + inputField, true,false);
pioconnection.Send("Chat", inputField);
inputField = "";
}
else{
sendTO = null;
privateChat[] hinges = FindObjectsOfType(typeof(privateChat)) as privateChat[];
foreach (privateChat hinge in hinges) {
if(inputField.Contains ("@"+hinge.transform.name)){
sendTO = hinge.transform.name;
}
}
if(sendTO != null){
ChatText( inputField, true,true);
pioconnection.Send("Private",sendTO, inputField);
inputField = "";
PrivateMsg = false;
sendTO = null;
}else{
ChatText("Invalid user, check username",false,false);
}
}
}
i modified the chat class to include a private bool
- Code: Select all
public class ChatEntryLobby {
public string text = "";
public bool mine = true;
public bool priv = false;
}
and included it in the chat entries:
- Code: Select all
void ChatText(string str, bool own, bool privat) {
var entry = new ChatEntryLobby();
entry.priv = privat;
entry.text = str;
entry.mine = own;
entries.Add(entry);
if(entries.Count > 50)
entries.RemoveAt(0);
scrollPosition.y = 1000000;
}
this is included in the fixedUptade to receive the Private call from the server.
- Code: Select all
case "Private":
if(m.GetString(1) == userid) {
ChatText(m.GetString(0) +" Private: " + m.GetString(2), true,true);
soundMan.Message("Private");
}
break;
soundMan" is a separate script i made to play a diferent sound when receive a public or private msg and is as simple as this:
- Code: Select all
public void Message(string Type){
switch (Type){
case "Public":
audio.PlayOneShot(publicMsg);
break;
case ("Private"):
audio.PlayOneShot(privateMsg);
break;
}
}
By last, this is the added case to the serverside script:
- Code: Select all
case "Private":
foreach (Player pl in Players){
if (pl.ConnectUserId != player.ConnectUserId){
pl.Send("Private", player.ConnectUserId, message.GetString(0), message.GetString(1));
}
}
break;
You can notice the msg is sent to all players in the room, and then is the client (player) who compares if he is the player the msg was sent to, otherwise it wont show it. The most eficient way would be to send the msg only to the person it was sended to, but i leave that open to anyone who wants to contribute with this. thanks