Chat System Setup
This commit is contained in:
42
Assets/Scripts/ChatBehaviour.cs
Normal file
42
Assets/Scripts/ChatBehaviour.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user