Skip to content

Commit c3b0d12

Browse files
committed
feat: Added start buttons to NetworkingManager inspector
1 parent 8588b8c commit c3b0d12

File tree

1 file changed

+70
-38
lines changed

1 file changed

+70
-38
lines changed

MLAPI-Editor/NetworkingManagerEditor.cs

Lines changed: 70 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Reflection;
44
using UnityEditor;
@@ -16,10 +16,10 @@ public class NetworkingManagerEditor : Editor
1616
private SerializedProperty dontDestroyOnLoadProperty;
1717
private SerializedProperty runInBackgroundProperty;
1818
private SerializedProperty logLevelProperty;
19-
19+
2020
// NetworkConfig
2121
private SerializedProperty networkConfigProperty;
22-
22+
2323
// NetworkConfig fields
2424
private SerializedProperty protocolVersionProperty;
2525
private SerializedProperty allowRuntimeSceneChanges;
@@ -51,10 +51,10 @@ public class NetworkingManagerEditor : Editor
5151

5252
private NetworkingManager networkingManager;
5353
private bool initialized;
54-
54+
5555
private readonly List<Type> transportTypes = new List<Type>();
56-
private string[] transportNames = new string[] {"Select transport..."};
57-
56+
private string[] transportNames = new string[] { "Select transport..." };
57+
5858
private void ReloadTransports()
5959
{
6060
transportTypes.Clear();
@@ -75,7 +75,7 @@ private void ReloadTransports()
7575
}
7676

7777
transportNames = new string[transportTypes.Count + 1];
78-
78+
7979
transportNames[0] = "Select transport...";
8080

8181
for (int i = 0; i < transportTypes.Count; i++)
@@ -91,13 +91,13 @@ private void Init()
9191

9292
initialized = true;
9393
networkingManager = (NetworkingManager)target;
94-
94+
9595
// Base properties
9696
dontDestroyOnLoadProperty = serializedObject.FindProperty("DontDestroy");
9797
runInBackgroundProperty = serializedObject.FindProperty("RunInBackground");
9898
logLevelProperty = serializedObject.FindProperty("LogLevel");
9999
networkConfigProperty = serializedObject.FindProperty("NetworkConfig");
100-
100+
101101
// NetworkConfig properties
102102
protocolVersionProperty = networkConfigProperty.FindPropertyRelative("ProtocolVersion");
103103
allowRuntimeSceneChanges = networkConfigProperty.FindPropertyRelative("AllowRuntimeSceneChanges");
@@ -123,7 +123,7 @@ private void Init()
123123
enableEncryptionProperty = networkConfigProperty.FindPropertyRelative("EnableEncryption");
124124
signKeyExchangeProperty = networkConfigProperty.FindPropertyRelative("SignKeyExchange");
125125
serverBase64PfxCertificateProperty = networkConfigProperty.FindPropertyRelative("ServerBase64PfxCertificate");
126-
126+
127127

128128
ReloadTransports();
129129
}
@@ -135,7 +135,7 @@ private void CheckNullProperties()
135135
runInBackgroundProperty = serializedObject.FindProperty("RunInBackground");
136136
logLevelProperty = serializedObject.FindProperty("LogLevel");
137137
networkConfigProperty = serializedObject.FindProperty("NetworkConfig");
138-
138+
139139
// NetworkConfig properties
140140
protocolVersionProperty = networkConfigProperty.FindPropertyRelative("ProtocolVersion");
141141
allowRuntimeSceneChanges = networkConfigProperty.FindPropertyRelative("AllowRuntimeSceneChanges");
@@ -225,10 +225,10 @@ public override void OnInspectorGUI()
225225
{
226226
Init();
227227
CheckNullProperties();
228-
228+
229229
{
230230
SerializedProperty iterator = serializedObject.GetIterator();
231-
231+
232232
for (bool enterChildren = true; iterator.NextVisible(enterChildren); enterChildren = false)
233233
{
234234
using (new EditorGUI.DisabledScope("m_Script" == iterator.propertyPath))
@@ -238,11 +238,11 @@ public override void OnInspectorGUI()
238238
}
239239
}
240240

241-
241+
242242
if (!networkingManager.IsServer && !networkingManager.IsClient)
243243
{
244244
serializedObject.Update();
245-
245+
246246
EditorGUILayout.PropertyField(dontDestroyOnLoadProperty);
247247
EditorGUILayout.PropertyField(runInBackgroundProperty);
248248
EditorGUILayout.PropertyField(logLevelProperty);
@@ -252,36 +252,36 @@ public override void OnInspectorGUI()
252252

253253
registeredScenesList.DoLayoutList();
254254
EditorGUILayout.Space();
255-
256-
255+
256+
257257
EditorGUILayout.LabelField("General", EditorStyles.boldLabel);
258258
EditorGUILayout.PropertyField(protocolVersionProperty);
259-
259+
260260
EditorGUILayout.PropertyField(networkTransportProperty);
261-
261+
262262
if (networkTransportProperty.objectReferenceValue == null)
263-
{
263+
{
264264
EditorGUILayout.HelpBox("You have no transport selected. A transport is required for the MLAPI to work. Which one do you want?", MessageType.Warning);
265265

266266
int selection = EditorGUILayout.Popup(0, transportNames);
267267

268268
if (selection > 0)
269269
{
270270
ReloadTransports();
271-
271+
272272
Component transport = networkingManager.gameObject.GetComponent(transportTypes[selection - 1]);
273-
273+
274274
if (transport == null)
275275
{
276276
transport = networkingManager.gameObject.AddComponent(transportTypes[selection - 1]);
277277
}
278-
278+
279279
networkTransportProperty.objectReferenceValue = transport;
280-
280+
281281
Repaint();
282282
}
283283
}
284-
284+
285285
EditorGUILayout.PropertyField(enableTimeResyncProperty);
286286
EditorGUILayout.PropertyField(timeResyncIntervalProperty);
287287

@@ -296,52 +296,84 @@ public override void OnInspectorGUI()
296296
{
297297
EditorGUILayout.PropertyField(maxObjectUpdatesPerTickProperty);
298298
EditorGUILayout.PropertyField(ensureNetworkedVarLengthSafetyProperty);
299-
}
300-
299+
}
300+
301301
EditorGUILayout.LabelField("Connection", EditorStyles.boldLabel);
302302
EditorGUILayout.PropertyField(connectionApprovalProperty);
303303

304304
using (new EditorGUI.DisabledScope(!networkingManager.NetworkConfig.ConnectionApproval))
305305
{
306306
EditorGUILayout.PropertyField(clientConnectionBufferTimeoutProperty);
307307
}
308-
308+
309309
EditorGUILayout.LabelField("Lag Compensation", EditorStyles.boldLabel);
310310
EditorGUILayout.PropertyField(secondsHistoryProperty);
311-
311+
312312
EditorGUILayout.LabelField("Spawning", EditorStyles.boldLabel);
313313
EditorGUILayout.PropertyField(forceSamePrefabsProperty);
314314
EditorGUILayout.PropertyField(usePrefabSyncProperty);
315-
315+
316316
EditorGUILayout.PropertyField(recycleNetworkIdsProperty);
317-
317+
318318
using (new EditorGUI.DisabledScope(!networkingManager.NetworkConfig.RecycleNetworkIds))
319319
{
320320
EditorGUILayout.PropertyField(networkIdRecycleDelayProperty);
321321
}
322-
322+
323323
EditorGUILayout.LabelField("Bandwidth", EditorStyles.boldLabel);
324324
EditorGUILayout.PropertyField(rpcHashSizeProperty);
325325

326326
EditorGUILayout.LabelField("Scene Management", EditorStyles.boldLabel);
327327
EditorGUILayout.PropertyField(loadSceneTimeOutProperty);
328328
EditorGUILayout.PropertyField(allowRuntimeSceneChanges);
329-
329+
330330
EditorGUILayout.LabelField("Cryptography", EditorStyles.boldLabel);
331331
EditorGUILayout.PropertyField(enableEncryptionProperty);
332332

333333
using (new EditorGUI.DisabledScope(!networkingManager.NetworkConfig.EnableEncryption))
334334
{
335335
EditorGUILayout.PropertyField(signKeyExchangeProperty);
336-
EditorGUILayout.PropertyField(serverBase64PfxCertificateProperty);
336+
EditorGUILayout.PropertyField(serverBase64PfxCertificateProperty);
337337
}
338-
338+
339339
serializedObject.ApplyModifiedProperties();
340+
341+
342+
// Start buttons below
343+
{
344+
string buttonDisabledReasonSuffix = "";
345+
346+
if (!EditorApplication.isPlaying)
347+
{
348+
buttonDisabledReasonSuffix = ". This can only be done in play mode";
349+
GUI.enabled = false;
350+
}
351+
352+
if (GUILayout.Button(new GUIContent("Start Host", "Starts a host instance" + buttonDisabledReasonSuffix)))
353+
{
354+
networkingManager.StartHost();
355+
}
356+
357+
if (GUILayout.Button(new GUIContent("Start Server", "Starts a server instance" + buttonDisabledReasonSuffix)))
358+
{
359+
networkingManager.StartServer();
360+
}
361+
362+
if (GUILayout.Button(new GUIContent("Start Client", "Starts a client instance" + buttonDisabledReasonSuffix)))
363+
{
364+
networkingManager.StartClient();
365+
}
366+
367+
if (!EditorApplication.isPlaying)
368+
{
369+
GUI.enabled = true;
370+
}
371+
}
340372
}
341373
else
342374
{
343375
string instanceType = "";
344-
376+
345377
if (networkingManager.IsHost)
346378
instanceType = "Host";
347379
else if (networkingManager.IsServer)
@@ -350,8 +382,8 @@ public override void OnInspectorGUI()
350382
instanceType = "Client";
351383

352384
EditorGUILayout.HelpBox("You cannot edit the NetworkConfig when a " + instanceType + " is running.", MessageType.Info);
353-
354-
if (GUILayout.Toggle(false, "Stop " + instanceType, EditorStyles.miniButtonMid))
385+
386+
if (GUILayout.Button(new GUIContent("Stop " + instanceType, "Stops the " + instanceType + " instance.")))
355387
{
356388
if (networkingManager.IsHost)
357389
networkingManager.StopHost();

0 commit comments

Comments
 (0)