80 lines
1.9 KiB
C#
Executable File
80 lines
1.9 KiB
C#
Executable File
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Mirror;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
using System;
|
|
|
|
public class playerChat : NetworkBehaviour
|
|
{
|
|
|
|
[SerializeField] private GameObject chatUI = null;
|
|
[SerializeField] private TMP_Text chatText = null;
|
|
[SerializeField] private TMP_InputField inputField = null;
|
|
|
|
private static event Action<string> OnMessage;
|
|
|
|
public override void OnStartAuthority()
|
|
{
|
|
base.OnStartAuthority();
|
|
Debug.Log("Chat has authorized");
|
|
chatUI.SetActive(true);
|
|
|
|
|
|
OnMessage += HandleNewMessage;
|
|
}
|
|
|
|
|
|
[ClientCallback]
|
|
private void OnDestroy()
|
|
{
|
|
if (!authority) { return; }
|
|
|
|
OnMessage -= HandleNewMessage;
|
|
}
|
|
|
|
private void HandleNewMessage(string message)
|
|
{
|
|
chatText.text += message;
|
|
}
|
|
|
|
[Client]
|
|
public void Send(string message)
|
|
{
|
|
if (!Input.GetKeyDown(KeyCode.Return)) { return; }
|
|
|
|
if (string.IsNullOrWhiteSpace(message)) { return; }
|
|
|
|
CmdSendMessage(message);
|
|
|
|
inputField.text = string.Empty;
|
|
}
|
|
|
|
[Client]
|
|
public void SendBtn(){
|
|
|
|
if (string.IsNullOrWhiteSpace(inputField.text)) { return; }
|
|
|
|
CmdSendMessage($"{gplayAuth.userNameCloud}: "+inputField.text);
|
|
|
|
inputField.text = string.Empty;
|
|
}
|
|
|
|
|
|
|
|
[Command]
|
|
private void CmdSendMessage(string message)
|
|
{
|
|
// RpcHandleMessage($"[{gplayAuth.userNameCloud}]: {message}");
|
|
RpcHandleMessage(message);
|
|
}
|
|
|
|
[ClientRpc]
|
|
private void RpcHandleMessage(string message)
|
|
{
|
|
OnMessage?.Invoke($"\n{message}");
|
|
}
|
|
|
|
|
|
}
|