Files

122 lines
3.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
// Client-only UI. DO NOT add a NetworkIdentity to this prefab.
public class ChatUI : MonoBehaviour
{
[Header("UI Elements")]
[SerializeField] private TextMeshProUGUI chatHistory;
[SerializeField] private Scrollbar scrollbar;
[SerializeField] private TMP_InputField chatMessage;
[SerializeField] private Button sendButton;
internal static string localPlayerName;
// Instance reference so static callers can check focus state safely
internal static ChatUI Instance;
private readonly List<string> lines = new();
private Player localPlayer;
void Awake()
{
Instance = this;
if (chatMessage != null)
{
chatMessage.onValueChanged.AddListener(ToggleButton);
chatMessage.onSubmit.AddListener(OnInputSubmit);
}
}
// Expose whether the chat input is focused so other systems can disable input/movement
public static bool IsInputFieldFocused => Instance != null && Instance.chatMessage != null && Instance.chatMessage.isFocused;
// Called by Player.OnStartLocalPlayer to connect the UI to the player's network methods.
public void Initialize(Player player)
{
localPlayer = player;
}
public void SetHistory(string[] history)
{
lines.Clear();
if (history != null && history.Length > 0)
lines.AddRange(history);
RefreshVisual();
}
// Called by Player.RpcReceiveChat / Player.TargetReceiveHistory
public void AddMessage(string message)
{
lines.Add(message);
if (chatHistory != null)
chatHistory.text += message + "\n";
StartCoroutine(ScrollNextFrame());
}
IEnumerator ScrollNextFrame()
{
yield return null;
yield return null;
if (scrollbar != null)
scrollbar.value = 0;
}
void RefreshVisual()
{
if (chatHistory == null) return;
chatHistory.text = string.Empty;
foreach (var l in lines)
chatHistory.text += l + "\n";
StartCoroutine(ScrollNextFrame());
}
public void ToggleButton(string input)
{
if (sendButton != null)
sendButton.interactable = !string.IsNullOrWhiteSpace(input);
}
private void OnInputSubmit(string input)
{
if (!string.IsNullOrWhiteSpace(input))
SendMessage();
}
public void SendMessage()
{
if (localPlayer == null)
{
Debug.LogWarning("ChatUI: local player not set; cannot send chat.");
return;
}
if (string.IsNullOrWhiteSpace(chatMessage?.text)) return;
localPlayer.SendChatToServer(chatMessage.text.Trim());
chatMessage.text = string.Empty;
chatMessage.ActivateInputField();
}
void OnDestroy()
{
if (chatMessage != null)
{
chatMessage.onValueChanged.RemoveListener(ToggleButton);
chatMessage.onSubmit.RemoveListener(OnInputSubmit);
}
// Clear static instance reference
if (Instance == this) Instance = null;
}
}