- Home /
Question by
Rasmus_Bonnesen · Jun 12, 2019 at 05:22 PM ·
c#networkingmultiplayerchat
Multiplayer text Chat, based on Command and ClientRpc not working.
I am currently working on a simple text chat for my multiplayer game. The problem is that the chat message only appears on the client that sends the message, but through my ingame Console, I can see that the Debug.Log in the ClientRpc gets called on every client, so every client gets the message, but only one of the clients (the client that sends the message) gets the message int the chat. Here is my text Chat script: Thanks for your help in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using TMPro;
public class SyncChat : NetworkBehaviour
{
[SerializeField] private TMP_InputField chatInputField;
[SerializeField] private GameObject chatInputFieldGO;
[SerializeField] private GameObject chatScrollGO;
[SerializeField] private ScrollRect chatScrollView;
[SerializeField] private KillFeed killFeedScript;
[SerializeField] private GameObject chatContentText;
[SerializeField] private GameObject chatScrollViewContent;
private float disableMSGTimer;
[SerializeField] private float msgVisibleTime = 9f;
string chatContent;
private Console console;
public bool chatIsOpen = false;
private string chatMSG;
void Start()
{
console = GameObject.FindWithTag("Console").GetComponent<Console>();
chatInputFieldGO.SetActive(false);
chatScrollGO.SetActive(true);
chatIsOpen = false;
}
[Client]
void Update()
{
if (!isLocalPlayer)
{
return;
}
if (KeyBindingManager.GetKeyDown(KeyAction.enter))
{
if (!console.isOn)
{
if (!chatIsOpen)
{
chatInputFieldGO.SetActive(true);
chatScrollGO.SetActive(true);
chatInputField.ActivateInputField();
chatIsOpen = true;
}
else
{
if (chatInputField.text != "")
{
if (chatInputField.text == "/clear")
{
SendMsgToChat("<color=green>Chat has been cleared</color>");
}
if (chatInputField.text != "/clear")
{
chatMSG = chatInputField.text;
CmdGetChatMSG(gameObject.GetComponent<Player>().localPlayerName, chatInputField.text);
}
}
chatInputField.text = "";
chatInputFieldGO.SetActive(false);
StopAllCoroutines();
StartCoroutine(ScrollDownCO());
//chatScroll.SetActive(false);
chatIsOpen = false;
}
}
}
}
[Command]
void CmdGetChatMSG(string _sourcePlayer, string _msg)
{
RpcSendMsgToAllPlayers("\n ["+_sourcePlayer+"] " +_msg);
}
[ClientRpc]
void RpcSendMsgToAllPlayers(string _msg)
{
chatScrollGO.SetActive(true);
SendMsgToChat(_msg);
Debug.Log("[From ClientRPC: " + _msg);
}
IEnumerator ScrollDownCO()
{
yield return new WaitForEndOfFrame();
chatScrollView.verticalNormalizedPosition = 0f;
yield return new WaitForSeconds(msgVisibleTime);
chatScrollGO.SetActive(false);
}
[SerializeField] private int maxMessages = 50;
List<TextChatMSG> textChatMessageList = new List<TextChatMSG>();
private void SendMsgToChat(string msgText)
{
StopAllCoroutines();
StartCoroutine(ScrollDownCO());
if(textChatMessageList.Count >= maxMessages)
{
Destroy(textChatMessageList[0].chatContentText.gameObject);
textChatMessageList.Remove(textChatMessageList[0]);
}
TextChatMSG newTextChatMessage = new TextChatMSG();
newTextChatMessage.msgText = msgText;
GameObject newTextItem = Instantiate(chatContentText, chatScrollViewContent.transform);
newTextChatMessage.chatContentText = newTextItem.GetComponent<TextMeshProUGUI>();
newTextChatMessage.chatContentText.text = newTextChatMessage.msgText;
textChatMessageList.Add(newTextChatMessage);
chatScrollView.verticalNormalizedPosition = 0f;
}
[System.Serializable]
public class TextChatMSG
{
public string msgText;
public TextMeshProUGUI chatContentText;
}
}
Comment