Files
Test-Multiplayer/Assets/Scripts/ChatBehaviour.cs
2026-01-10 20:13:22 +02:00

49 lines
1.2 KiB
C#

using Mirror;
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ChatBehaviour : NetworkBehaviour
{
[SerializeField] private TextMeshProUGUI chatText = null;
[SerializeField] private TMP_InputField inputField = null;
[SerializeField] private GameObject canvas = null;
public override void OnStartLocalPlayer()
{
canvas.SetActive(true);
SpawnScript.OnMessage += HandleNewMessage;
}
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;
}
}