113 lines
4.1 KiB
C#
113 lines
4.1 KiB
C#
using FishNet;
|
|
using FishNet.Connection;
|
|
using FishNet.Transporting;
|
|
using Managers;
|
|
using TMPro;
|
|
using Types.Broadcasts;
|
|
using UnityEngine;
|
|
|
|
namespace UI
|
|
{
|
|
public class NetworkChatUIController : MonoBehaviour
|
|
|
|
|
|
{
|
|
[SerializeField] private TMP_InputField messageInput;
|
|
|
|
// prefab of the message format that is displayed
|
|
[SerializeField] private GameObject messageDisplayPrefab;
|
|
[SerializeField] private GameObject messageParentContainer;
|
|
|
|
private void Start()
|
|
{
|
|
Debug.Log("NetworkChatUIController Start");
|
|
InstanceFinder.ServerManager.OnServerConnectionState += OnServerConnectionState;
|
|
InstanceFinder.ClientManager.OnClientConnectionState += OnClientConnectionState;
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
// InstanceFinder.ClientManager.RegisterBroadcast<ChatBroadcast>(OnChatBroadcast);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
InstanceFinder.ClientManager.UnregisterBroadcast<ChatBroadcast>(OnChatBroadcast);
|
|
}
|
|
|
|
|
|
public void OnClick_Send()
|
|
{
|
|
var chatBroadcast = new ChatBroadcast
|
|
{
|
|
Message = messageInput.text,
|
|
NetworkTime = Time.time,
|
|
Username = GameManager.Instance.GetCurrentPlayer().PlayerName,
|
|
FontColor = Color.white
|
|
};
|
|
|
|
Debug.Log("Sending chat message: " + chatBroadcast.Message);
|
|
InstanceFinder.ClientManager.Broadcast(chatBroadcast);
|
|
messageInput.text = "";
|
|
|
|
Debug.Log("Sent message");
|
|
}
|
|
|
|
|
|
private void OnServerConnectionState(ServerConnectionStateArgs obj)
|
|
{
|
|
Debug.Log("Server Connection State Changed " + obj.ConnectionState);
|
|
if (obj.ConnectionState == LocalConnectionState.Started)
|
|
{
|
|
Debug.Log("Registering server broadcast");
|
|
InstanceFinder.ServerManager.RegisterBroadcast<ChatBroadcast>(OnServerChatBroadcast);
|
|
}
|
|
}
|
|
|
|
private void OnServerChatBroadcast(NetworkConnection connection, ChatBroadcast broadcast, Channel channel)
|
|
{
|
|
Debug.Log("ChatBroadcast received (server)");
|
|
Debug.Log("ChatBroadcast received (server) from: " + broadcast.Username + " at " + broadcast.NetworkTime);
|
|
|
|
InstanceFinder.ServerManager.Broadcast(broadcast);
|
|
}
|
|
|
|
|
|
private void OnClientConnectionState(ClientConnectionStateArgs obj)
|
|
{
|
|
Debug.Log("netchat-ui: Client Connection State Changed " + obj.ConnectionState);
|
|
var clientState = obj.ConnectionState;
|
|
if (clientState == LocalConnectionState.Started)
|
|
{
|
|
Debug.Log("Registering client for broadcast");
|
|
InstanceFinder.ClientManager.RegisterBroadcast<ChatBroadcast>(OnChatBroadcast);
|
|
}
|
|
else if (clientState == LocalConnectionState.Stopped)
|
|
{
|
|
Debug.Log("Unregistering client for broadcast");
|
|
InstanceFinder.ClientManager.UnregisterBroadcast<ChatBroadcast>(OnChatBroadcast);
|
|
}
|
|
}
|
|
|
|
private void OnChatBroadcast(ChatBroadcast broadcast, Channel channel)
|
|
{
|
|
Debug.Log("ChatBroadcast received");
|
|
// Debug.Log(broadcast.Message, broadcast.FontColor, broadcast.Username);
|
|
|
|
Debug.Log("ChatBroadcast received from: " + broadcast.Username + " at " + broadcast.NetworkTime);
|
|
Debug.Log("ChatBroadcast message: " + broadcast.Message);
|
|
AddMessageToUI(broadcast);
|
|
}
|
|
|
|
private void AddMessageToUI(ChatBroadcast broadcast)
|
|
{
|
|
var messageDisplay = Instantiate(messageDisplayPrefab, messageParentContainer.transform);
|
|
|
|
var nameText = (TMP_Text)messageDisplay.GetComponentsInChildren(typeof(TMP_Text), true)[0];
|
|
nameText.text = broadcast.Username;
|
|
var messageText = (TMP_Text)messageDisplay.GetComponentsInChildren(typeof(TMP_Text), true)[1];
|
|
messageText.text = broadcast.Message;
|
|
messageText.color = broadcast.FontColor;
|
|
}
|
|
}
|
|
} |