forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderPlayerListMb.cs
More file actions
276 lines (248 loc) · 11.6 KB
/
Copy pathRenderPlayerListMb.cs
File metadata and controls
276 lines (248 loc) · 11.6 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Unity.Collections;
using Unity.Entities;
using UnityEngine;
namespace Unity.NetCode.Samples.PlayerList
{
/// <summary>Simple IMGUI example rendering of a player scoreboard with connect/disconnect notifications.</summary>
public class RenderPlayerListMb : MonoBehaviour
{
public AnimationCurve notificationOpacityCurve = AnimationCurve.Linear(0, 1, 1, 0);
public GUISkin skin;
public bool debugShowThinClients;
const int k_ScoreboardWidth = 220;
const int k_NetIdWidth = 33;
static readonly GUILayoutOption s_UsernameWidth = GUILayout.Width(k_ScoreboardWidth-(k_NetIdWidth+5));
static readonly GUILayoutOption s_NetIdWidth = GUILayout.Width(k_NetIdWidth);
static readonly GUILayoutOption s_ScoreboardWidth = GUILayout.Width(k_ScoreboardWidth);
static readonly GUILayoutOption s_NotificationWidth = GUILayout.Width(k_ScoreboardWidth);
Dictionary<World, WorldCache> m_WorldCaches = new Dictionary<World, WorldCache>(1);
ulong m_LastSequenceNumber;
private int m_ScreenWidth;
private int m_ScreenHeight;
class WorldCache
{
public EntityQuery NetworkIdQuery;
public EntityQuery DesiredUsernameQuery;
public EntityQuery PlayerListNotificationBufferQuery;
public EntityQuery PlayerListBufferEntryQuery;
public string DesiredUsername;
public bool IsModifying;
}
void Update()
{
if (World.NextSequenceNumber != m_LastSequenceNumber)
{
foreach (var world in World.All)
{
if(world.SequenceNumber >= m_LastSequenceNumber && world.IsClient())
{
if (!world.IsThinClient() || (debugShowThinClients && world.IsThinClient()))
{
if (!m_WorldCaches.ContainsKey(world))
{
m_WorldCaches[world] = new WorldCache
{
NetworkIdQuery = world.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<NetworkId>()),
DesiredUsernameQuery = world.EntityManager.CreateEntityQuery(ComponentType.ReadWrite<DesiredUsername>()),
PlayerListBufferEntryQuery = world.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<PlayerListBufferEntry>()),
PlayerListNotificationBufferQuery = world.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<PlayerListNotificationBuffer>())
};
}
}
}
}
m_LastSequenceNumber = World.NextSequenceNumber;
CleanUpWorldCache();
}
}
void CleanUpWorldCache()
{
bool repeat;
do
{
repeat = false;
foreach (var kvp in m_WorldCaches)
{
if (!kvp.Key.IsCreated)
{
m_WorldCaches.Remove(kvp.Key);
repeat = true;
break;
}
}
} while (repeat);
}
void OnGUI()
{
m_ScreenWidth = Screen.width;
m_ScreenHeight = Screen.height;
GUI.skin = skin;
GUILayout.BeginHorizontal();
{
// Clients:
foreach (var worldKvp in m_WorldCaches)
{
GUILayout.BeginVertical();
DrawPlayerListForClientWorld(worldKvp);
GUILayout.EndVertical();
if (IsGuiFullyOffScreen()) break;
}
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
void DrawPlayerListForClientWorld(KeyValuePair<World, WorldCache> kvp)
{
var (world, cache) = kvp;
if (!world.IsCreated) { return; }
if (cache.DesiredUsernameQuery.IsEmptyIgnoreFilter) { return; }
GUI.color = Color.yellow;
if(debugShowThinClients) GUILayout.Box(world.Name);
GUILayout.Box("PLAYER LIST", s_ScoreboardWidth);
if (!cache.IsModifying)
cache.DesiredUsername = cache.DesiredUsernameQuery.GetSingleton<DesiredUsername>().Value.ToString();
var newNameString = GUILayout.TextField(cache.DesiredUsername, s_ScoreboardWidth);
if (newNameString != cache.DesiredUsername)
{
cache.DesiredUsername = newNameString;
cache.IsModifying = true;
}
if (cache.IsModifying)
{
var hasNoControlFocus = GUIUtility.keyboardControl == 0;
var returnKeyPressed = Event.current.keyCode is KeyCode.Return or KeyCode.KeypadEnter;
if (hasNoControlFocus || returnKeyPressed)
{
ref var desiredUsernameStoreRw = ref cache.DesiredUsernameQuery.GetSingletonRW<DesiredUsername>().ValueRW;
FixedStringMethods.CopyFromTruncated(ref desiredUsernameStoreRw.Value, newNameString);
}
}
GUI.color = Color.grey;
var networkId = cache.NetworkIdQuery.IsEmptyIgnoreFilter ? -1 : cache.NetworkIdQuery.GetSingleton<NetworkId>().Value;
if (networkId <= 0)
{
GUILayout.Box("Disconnected.");
return;
}
DrawPlayerList(cache);
DrawPlayerNotifications(cache);
}
void DrawPlayerNotifications(WorldCache worldCache)
{
if (worldCache.PlayerListNotificationBufferQuery.IsEmptyIgnoreFilter) return;
GUILayout.EndVertical();
GUILayout.BeginVertical();
GUI.color = Color.yellow;
GUILayout.Box("NOTIFICATIONS", s_NotificationWidth);
var notifications = worldCache.PlayerListNotificationBufferQuery.GetSingletonBuffer<PlayerListNotificationBuffer>();
GUI.color = Color.white;
foreach (var entry in notifications)
{
DrawNotification(entry);
if (IsGuiFullyOffScreen()) break;
}
}
void DrawPlayerList(WorldCache cache)
{
if (cache.PlayerListBufferEntryQuery.IsEmptyIgnoreFilter)
{
GUI.color = Color.red;
GUILayout.Box($"No PlayerListBufferEntry!", s_ScoreboardWidth);
return;
}
var playerEntries = cache.PlayerListBufferEntryQuery.GetSingletonBuffer<PlayerListBufferEntry>();
GUILayout.Box($"{PlayerListBufferEntry.CountNumConnectedPlayers(playerEntries)} Players in Game", s_ScoreboardWidth);
for (var i = 0; i < playerEntries.Length; i++)
{
var entry = playerEntries[i];
if (!entry.IsCreated || !entry.State.IsConnected) continue;
GUILayout.BeginHorizontal();
{
GUI.color = Color.grey;
GUILayout.Box(entry.State.NetworkId.ToString(), s_NetIdWidth);
GUI.color = Color.white;
GUILayout.Box(entry.State.Username.Value.Value, s_UsernameWidth);
}
GUILayout.EndHorizontal();
if (IsGuiFullyOffScreen()) break;
}
}
private bool IsGuiFullyOffScreen()
{
var min = GUILayoutUtility.GetLastRect().min;
return min.x > m_ScreenWidth || min.y > m_ScreenHeight;
}
void DrawNotification(PlayerListNotificationBuffer entry)
{
string notificationText;
var username = entry.Event.Username.Value;
Color color;
switch (entry.Event.ChangeType)
{
case PlayerListEntry.ChangedRpc.UpdateType.PlayerDisconnect:
switch (entry.Event.Reason)
{
case NetworkStreamDisconnectReason.Timeout:
notificationText = $"{username} timed out!";
color = Color.red;
break;
case NetworkStreamDisconnectReason.MaxConnectionAttempts:
notificationText = $"{username} exceeded max connection attempts!";
color = Color.red;
break;
case NetworkStreamDisconnectReason.ClosedByRemote:
notificationText = $"{username} quit!";
color = new Color(1f, 0.39f, 0.43f);
break;
case NetworkStreamDisconnectReason.ConnectionClose:
notificationText = $"{username} was disconnected by server!";
color = new Color(1f, 0.39f, 0.43f);
break;
case NetworkStreamDisconnectReason.InvalidRpc:
notificationText = $"{username} had invalid RPC!";
color = new Color(0.39f, 0.01f, 0.63f);
break;
case NetworkStreamDisconnectReason.BadProtocolVersion:
notificationText = $"{username} had invalid protocol version!";
color = new Color(0.39f, 0.01f, 0.63f);
break;
case NetworkStreamDisconnectReason.AuthenticationFailure:
notificationText = $"{username} could not be authenticated!";
color = new Color(0.39f, 0.01f, 0.63f);
break;
case NetworkStreamDisconnectReason.ProtocolError:
notificationText = $"{username} had a low-level transport error!";
color = new Color(0.39f, 0.01f, 0.63f);
break;
default:
notificationText = $"{username} disconnected with error {(int) entry.Event.Reason}!";
color = new Color(0.39f, 0.01f, 0.63f);
break;
}
break;
case PlayerListEntry.ChangedRpc.UpdateType.NewJoiner:
notificationText = $"{username} connected!";
color = Color.green;
break;
case PlayerListEntry.ChangedRpc.UpdateType.ExistingPlayer:
notificationText = $"{username} already here!";
color = new Color(0f, 0.89f, 1f);
break;
case PlayerListEntry.ChangedRpc.UpdateType.UsernameChange:
notificationText = $"{username} changed names!";
color = Color.white;
break;
default:
notificationText = $"{username} made unrecognised change {entry.Event.ChangeType}!";
color = Color.red;
break;
}
var targetAlpha = notificationOpacityCurve.Evaluate(entry.DurationLeft);
GUI.color = Color.LerpUnclamped(Color.clear, color, targetAlpha);
GUILayout.Box(notificationText, s_NotificationWidth);
}
}
}