Chat System Setup

This commit is contained in:
pelpanagiotis
2025-12-23 21:43:28 +02:00
parent 329b7d3183
commit 6262ec6b61
3474 changed files with 425014 additions and 28055 deletions

View File

@@ -0,0 +1,42 @@
using Mirror;
using System;
using UnityEngine;
using TMPro;
public class ChatBehaviour : NetworkBehaviour
{
[SerializeField] private TextMeshProUGUI chatText = null;
[SerializeField] private TMP_InputField inputField = null;
[SerializeField] private GameObject canvas = null;
public override void OnStartClient()
{
canvas.SetActive(true);
SpawnScript.OnMessage += HandleNewMessage;
}
private void OnDestroy()
{
SpawnScript.OnMessage -= HandleNewMessage;
}
private void HandleNewMessage(string message)
{
chatText.text += message;
}
[Client]
public void Send()
{
if (!authority)
{
Debug.LogWarning("Attempted to send chat without authority.");
}
if (string.IsNullOrWhiteSpace(inputField.text)) { return; }
NetworkClient.localPlayer
.GetComponent<SpawnScript>()
.CmdSendMessage(inputField.text);
inputField.text = string.Empty;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3e2cbf076f9d6ec41b63cd8c0fe4c17d

View File

@@ -0,0 +1,28 @@
using Mirror;
using System;
using UnityEngine;
public class SpawnScript : NetworkBehaviour
{
public GameObject chatPrefab;
public static event Action<string> OnMessage;
public override void OnStartServer()
{
GameObject obj = Instantiate(chatPrefab);
NetworkServer.Spawn(obj);
}
[Command]
public void CmdSendMessage(string message)
{
RpcHandleMessage($"[{connectionToClient.connectionId}]: {message}");
}
[ClientRpc]
private void RpcHandleMessage(string message)
{
OnMessage?.Invoke($"\n{message}");
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6dab1be56f31ff44889b274b8f565102