-
-
Notifications
You must be signed in to change notification settings - Fork 862
Expand file tree
/
Copy pathChatUI.cs
More file actions
100 lines (84 loc) · 3.15 KB
/
Copy pathChatUI.cs
File metadata and controls
100 lines (84 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Mirror.Examples.Chat
{
public class ChatUI : NetworkBehaviour
{
[Header("UI Elements")]
[SerializeField] Text chatHistory;
[SerializeField] Scrollbar scrollbar;
[SerializeField] InputField chatMessage;
[SerializeField] Button sendButton;
// This is only set on client to the name of the local player
internal static string localPlayerName;
// Server-only cross-reference of connections to player names
internal static readonly Dictionary<NetworkConnectionToClient, string> connNames = new Dictionary<NetworkConnectionToClient, string>();
public override void OnStartServer()
{
connNames.Clear();
}
public override void OnStartClient()
{
chatHistory.text = "";
}
[Command(requiresAuthority = false)]
void CmdSend(string message, NetworkConnectionToClient sender = null)
{
if (!connNames.ContainsKey(sender))
connNames.Add(sender, sender.identity.GetComponent<Player>().playerName);
if (!string.IsNullOrWhiteSpace(message))
RpcReceive(connNames[sender], message.Trim());
}
[ClientRpc]
void RpcReceive(string playerName, string message)
{
string prettyMessage = playerName == localPlayerName ?
$"<color=red>{playerName}:</color> {message}" :
$"<color=blue>{playerName}:</color> {message}";
AppendMessage(prettyMessage);
}
void AppendMessage(string message)
{
StartCoroutine(AppendAndScroll(message));
}
IEnumerator AppendAndScroll(string message)
{
chatHistory.text += message + "\n";
// it takes 2 frames for the UI to update ?!?!
yield return null;
yield return null;
// slam the scrollbar down
scrollbar.value = 0;
}
// Called by UI element ExitButton.OnClick
public void ExitButtonOnClick()
{
// StopHost calls both StopClient and StopServer
// StopServer does nothing on remote clients
NetworkManager.Instance.StopHost();
}
// Called by UI element MessageField.OnValueChanged
public void ToggleButton(string input)
{
sendButton.interactable = !string.IsNullOrWhiteSpace(input);
}
// Called by UI element MessageField.OnEndEdit
public void OnEndEdit(string input)
{
if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter) || Input.GetButtonDown("Submit"))
SendMessage();
}
// Called by OnEndEdit above and UI element SendButton.OnClick
public void SendMessage()
{
if (!string.IsNullOrWhiteSpace(chatMessage.text))
{
CmdSend(chatMessage.text.Trim());
chatMessage.text = string.Empty;
chatMessage.ActivateInputField();
}
}
}
}