Chat Refactor + Spawn Points Y Axis Fix
This commit is contained in:
107
Assets/Scripts/Chat/ChatUI.cs
Normal file
107
Assets/Scripts/Chat/ChatUI.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
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;
|
||||
|
||||
private readonly List<string> lines = new();
|
||||
private Player localPlayer;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (chatMessage != null)
|
||||
{
|
||||
chatMessage.onValueChanged.AddListener(ToggleButton);
|
||||
chatMessage.onSubmit.AddListener(OnInputSubmit);
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user