-
Notifications
You must be signed in to change notification settings - Fork 460
Expand file tree
/
Copy pathExtendedNetworkManager.cs
More file actions
427 lines (369 loc) · 13.4 KB
/
ExtendedNetworkManager.cs
File metadata and controls
427 lines (369 loc) · 13.4 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Unity.Netcode;
using Unity.Services.Authentication;
using Unity.Services.Core;
using Unity.Services.Multiplayer;
using UnityEngine;
using SessionState = Unity.Services.Multiplayer.SessionState;
#if UNITY_EDITOR
using Unity.Netcode.Editor;
using UnityEditor;
/// <summary>
/// The custom editor for the <see cref="ExtendedNetworkManager"/> component.
/// </summary>
[CustomEditor(typeof(ExtendedNetworkManager), true)]
[CanEditMultipleObjects]
public class ExtendedNetworkManagerEditor : NetworkManagerEditor
{
private SerializedProperty m_ConnectionType;
private SerializedProperty m_TargetFrameRate;
private SerializedProperty m_EnableVSync;
public override void OnEnable()
{
m_ConnectionType = serializedObject.FindProperty(nameof(ExtendedNetworkManager.ConnectionType));
m_TargetFrameRate = serializedObject.FindProperty(nameof(ExtendedNetworkManager.TargetFrameRate));
m_EnableVSync = serializedObject.FindProperty(nameof(ExtendedNetworkManager.EnableVSync));
base.OnEnable();
}
private void DisplayExtendedNetworkManagerProperties()
{
EditorGUILayout.PropertyField(m_ConnectionType);
EditorGUILayout.PropertyField(m_TargetFrameRate);
EditorGUILayout.PropertyField(m_EnableVSync);
}
public override void OnInspectorGUI()
{
var extendedNetworkManager = target as ExtendedNetworkManager;
// Handle switching the appropriate connection type based on the network topology
// Host connectio type can be set for client-server and distributed authority
// Live Service can only be used with distributed authority
// Client-server can only be used with a host connection type
var connectionTypes = Enum.GetValues(typeof(ExtendedNetworkManager.ConnectionTypes));
var connectionType = ExtendedNetworkManager.ConnectionTypes.LiveService;
if (m_ConnectionType.enumValueIndex > 0 && m_ConnectionType.enumValueIndex < connectionTypes.Length)
{
connectionType = (ExtendedNetworkManager.ConnectionTypes)connectionTypes.GetValue(m_ConnectionType.enumValueIndex);
}
void SetExpanded(bool expanded) { extendedNetworkManager.ExtendedNetworkManagerExpanded = expanded; };
DrawFoldOutGroup<ExtendedNetworkManager>(extendedNetworkManager.GetType(), DisplayExtendedNetworkManagerProperties, extendedNetworkManager.ExtendedNetworkManagerExpanded, SetExpanded);
var updatedConnectedType = (ExtendedNetworkManager.ConnectionTypes)connectionTypes.GetValue(m_ConnectionType.enumValueIndex);
if (connectionType == updatedConnectedType && updatedConnectedType == ExtendedNetworkManager.ConnectionTypes.LiveService && extendedNetworkManager.NetworkConfig.NetworkTopology == NetworkTopologyTypes.ClientServer)
{
extendedNetworkManager.ConnectionType = ExtendedNetworkManager.ConnectionTypes.Host;
}
else if (connectionType == ExtendedNetworkManager.ConnectionTypes.Host && updatedConnectedType == ExtendedNetworkManager.ConnectionTypes.LiveService && extendedNetworkManager.NetworkConfig.NetworkTopology == NetworkTopologyTypes.ClientServer)
{
extendedNetworkManager.NetworkConfig.NetworkTopology = NetworkTopologyTypes.DistributedAuthority;
}
base.OnInspectorGUI();
}
}
#endif
public class ExtendedNetworkManager : NetworkManager
{
#if UNITY_EDITOR
// Inspector view expand/collapse settings for this derived child class
[HideInInspector]
public bool ExtendedNetworkManagerExpanded;
#endif
public static ExtendedNetworkManager Instance;
public enum ConnectionTypes
{
LiveService,
Host,
}
public ConnectionTypes ConnectionType;
public int TargetFrameRate = 100;
public bool EnableVSync = false;
[HideInInspector]
[SerializeField]
private int m_OriginalVSyncCount;
#if UNITY_EDITOR
protected override void OnValidateComponent()
{
m_OriginalVSyncCount = QualitySettings.vSyncCount;
base.OnValidateComponent();
}
#endif
private ISession m_CurrentSession;
private string m_SessionName;
private string m_ProfileName;
private Task m_SessionTask;
private enum ConnectionStates
{
None,
Connecting,
Connected,
}
private ConnectionStates m_ConnectionState;
public static string GetRandomString(int length)
{
var r = new System.Random();
return new string(Enumerable.Range(0, length).Select(_ => (char)r.Next('a', 'z')).ToArray());
}
public void SetFrameRate(int targetFrameRate, bool enableVsync)
{
Application.targetFrameRate = targetFrameRate;
QualitySettings.vSyncCount = enableVsync ? m_OriginalVSyncCount : 0;
}
private void Awake()
{
Screen.SetResolution((int)(Screen.currentResolution.width * 0.40f), (int)(Screen.currentResolution.height * 0.40f), FullScreenMode.Windowed);
SetFrameRate(TargetFrameRate, EnableVSync);
SetSingleton();
}
private async void Start()
{
OnClientConnectedCallback += OnClientConnected;
OnClientDisconnectCallback += OnClientDisconnect;
OnConnectionEvent += OnClientConnectionEvent;
if (UnityServices.Instance != null && UnityServices.Instance.State != ServicesInitializationState.Initialized)
{
await UnityServices.InitializeAsync();
}
if (!AuthenticationService.Instance.IsSignedIn)
{
AuthenticationService.Instance.SignInFailed += SignInFailed;
AuthenticationService.Instance.SignedIn += SignedIn;
if (string.IsNullOrEmpty(m_ProfileName))
{
m_ProfileName = GetRandomString(5);
}
AuthenticationService.Instance.SwitchProfile(m_ProfileName);
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
}
private void OnDestroy()
{
OnClientConnectedCallback -= OnClientConnected;
OnClientDisconnectCallback -= OnClientDisconnect;
OnConnectionEvent -= OnClientConnectionEvent;
}
private void SignedIn()
{
AuthenticationService.Instance.SignedIn -= SignedIn;
Debug.Log($"Signed in anonymously with profile {m_ProfileName}");
}
private void SignInFailed(RequestFailedException error)
{
AuthenticationService.Instance.SignInFailed -= SignInFailed;
Debug.LogError($"Failed to sign in {m_ProfileName} anonymously: {error}");
}
private void OnDrawLiveServiceGUI()
{
m_SessionName = GUILayout.TextField(m_SessionName);
if (GUILayout.Button("Create or Connect To Session"))
{
NetworkConfig.UseCMBService = true;
OnClientStopped += ClientStopped;
OnClientStarted += ClientStarted;
m_SessionTask = ConnectThroughLiveService();
m_ConnectionState = ConnectionStates.Connecting;
LogMessage($"Connecting to session {m_SessionName}...");
}
}
private void OnDrawDAHostGUI()
{
if (GUILayout.Button("Start Host"))
{
OnClientStopped += ClientStopped;
OnClientStarted += ClientStarted;
StartHost();
}
if (GUILayout.Button("Start Client"))
{
OnClientStopped += ClientStopped;
OnClientStarted += ClientStarted;
StartClient();
}
}
private void OnUpdateGUIDisconnected()
{
GUILayout.BeginArea(new Rect(10, 10, 300, 800));
GUILayout.Label("Session Name", GUILayout.Width(100));
var connectionType = ConnectionType;
if (NetworkConfig.NetworkTopology == NetworkTopologyTypes.ClientServer && connectionType != ConnectionTypes.Host)
{
connectionType = ConnectionTypes.Host;
}
switch (connectionType)
{
case ConnectionTypes.LiveService:
{
OnDrawLiveServiceGUI();
break;
}
case ConnectionTypes.Host:
{
OnDrawDAHostGUI();
break;
}
}
GUILayout.EndArea();
GUILayout.BeginArea(new Rect(10, Display.main.renderingHeight - 40, Display.main.renderingWidth - 10, 30));
var scenesPreloaded = new System.Text.StringBuilder();
scenesPreloaded.Append("Scenes Preloaded: ");
for (int i = 0; i < UnityEngine.SceneManagement.SceneManager.sceneCount; i++)
{
var scene = UnityEngine.SceneManagement.SceneManager.GetSceneAt(i);
scenesPreloaded.Append($"[{scene.name}]");
}
GUILayout.Label(scenesPreloaded.ToString());
GUILayout.EndArea();
}
private void OnUpdateGUIConnected()
{
if (CMBServiceConnection)
{
GUILayout.BeginArea(new Rect(10, 10, 800, 800));
GUILayout.Label($"Session: {m_SessionName}");
GUILayout.EndArea();
}
else
{
GUILayout.BeginArea(new Rect(10, 10, 800, 800));
if (DistributedAuthorityMode)
{
GUILayout.Label($"DAHosted Session");
}
else
{
GUILayout.Label($"Client-Server Session");
}
GUILayout.EndArea();
}
GUILayout.BeginArea(new Rect(Display.main.renderingWidth - 160, 10, 150, 80));
if (GUILayout.Button("Disconnect"))
{
if (m_CurrentSession != null && m_CurrentSession.State == SessionState.Connected)
{
m_CurrentSession.LeaveAsync();
m_CurrentSession = null;
}
else
{
Shutdown();
}
}
GUILayout.EndArea();
}
private void OnGUI()
{
var yAxisOffset = 10;
switch (m_ConnectionState)
{
case ConnectionStates.None:
{
yAxisOffset = 80;
OnUpdateGUIDisconnected();
break;
}
case ConnectionStates.Connected:
{
yAxisOffset = 40;
OnUpdateGUIConnected();
break;
}
}
GUILayout.BeginArea(new Rect(10, yAxisOffset, 600, 800));
if (m_MessageLogs.Count > 0)
{
GUILayout.Label("-----------(Log)-----------");
// Display any messages logged to screen
foreach (var messageLog in m_MessageLogs)
{
GUILayout.Label(messageLog.Message);
}
GUILayout.Label("---------------------------");
}
GUILayout.EndArea();
}
private void ClientStarted()
{
OnClientStarted -= ClientStarted;
m_ConnectionState = ConnectionStates.Connected;
LogMessage($"Connected to session {m_SessionName}.");
}
private void ClientStopped(bool isHost)
{
OnClientStopped -= ClientStopped;
m_ConnectionState = ConnectionStates.None;
m_SessionTask = null;
m_CurrentSession = null;
}
private async Task<ISession> ConnectThroughLiveService()
{
try
{
var options = new SessionOptions()
{
Name = m_SessionName,
MaxPlayers = 32
}.WithDistributedAuthorityNetwork();
m_CurrentSession = await MultiplayerService.Instance.CreateOrJoinSessionAsync(m_SessionName, options);
return m_CurrentSession;
}
catch (Exception e)
{
LogMessage($"{e.Message}");
Debug.LogException(e);
}
return null;
}
private void Update()
{
if (m_MessageLogs.Count == 0)
{
return;
}
for (int i = m_MessageLogs.Count - 1; i >= 0; i--)
{
if (m_MessageLogs[i].ExpirationTime < Time.realtimeSinceStartup)
{
m_MessageLogs.RemoveAt(i);
}
}
}
private void OnClientConnectionEvent(NetworkManager networkManager, ConnectionEventData eventData)
{
LogMessage($"[{Time.realtimeSinceStartup}] Connection event {eventData.EventType} for Client-{eventData.ClientId}.");
}
private void OnClientConnected(ulong clientId)
{
LogMessage($"[{Time.realtimeSinceStartup}] Connected event invoked for Client-{clientId}.");
}
private void OnClientDisconnect(ulong clientId)
{
LogMessage($"[{Time.realtimeSinceStartup}] Disconnected event invoked for Client-{clientId}.");
}
private List<MessageLog> m_MessageLogs = new List<MessageLog>();
private class MessageLog
{
public string Message { get; private set; }
public float ExpirationTime { get; private set; }
public MessageLog(string msg, float timeToLive)
{
Message = msg;
ExpirationTime = Time.realtimeSinceStartup + timeToLive;
}
}
public void LogMessage(string msg, float timeToLive = 10.0f)
{
if (m_MessageLogs.Count > 0)
{
m_MessageLogs.Insert(0, new MessageLog(msg, timeToLive));
}
else
{
m_MessageLogs.Add(new MessageLog(msg, timeToLive));
}
Debug.Log(msg);
}
public ExtendedNetworkManager()
{
Instance = this;
}
}