Skip to content

Commit 4b8b88d

Browse files
committed
Added scene cleanup & checks for already running instances
1 parent 6ad088e commit 4b8b88d

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,12 @@ private ConnectionConfig Init()
365365
/// <param name="netConfig">The NetworkingConfiguration to use</param>
366366
public void StartServer()
367367
{
368+
if (isServer || isClient)
369+
{
370+
Debug.LogWarning("MLAPI: Cannot start server while an instance is already running");
371+
return;
372+
}
373+
368374
ConnectionConfig cConfig = Init();
369375
if (NetworkConfig.ConnectionApproval)
370376
{
@@ -389,6 +395,12 @@ public void StartServer()
389395
/// <param name="netConfig">The NetworkingConfiguration to use</param>
390396
public void StartClient()
391397
{
398+
if (isServer || isClient)
399+
{
400+
Debug.LogWarning("MLAPI: Cannot start client while an instance is already running");
401+
return;
402+
}
403+
392404
ConnectionConfig cConfig = Init();
393405
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
394406
hostId = NetworkTransport.AddHost(hostTopology, 0, null);
@@ -453,6 +465,11 @@ public void StopClient()
453465
/// <param name="netConfig">The NetworkingConfiguration to use</param>
454466
public void StartHost()
455467
{
468+
if (isServer || isClient)
469+
{
470+
Debug.LogWarning("MLAPI: Cannot start host while an instance is already running");
471+
return;
472+
}
456473
ConnectionConfig cConfig = Init();
457474
if (NetworkConfig.ConnectionApproval)
458475
{
@@ -502,6 +519,7 @@ private void Shutdown()
502519
isListening = false;
503520
_isClient = false;
504521
_isServer = false;
522+
SpawnManager.DestroyNonSceneObjects();
505523
NetworkTransport.Shutdown();
506524
}
507525

MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ internal static void DestroySceneObjects()
7575
}
7676
}
7777

78+
internal static void DestroyNonSceneObjects()
79+
{
80+
foreach (KeyValuePair<uint, NetworkedObject> netObject in spawnedObjects)
81+
{
82+
if (!netObject.Value.sceneObject)
83+
MonoBehaviour.Destroy(netObject.Value.gameObject);
84+
}
85+
}
86+
7887
internal static GameObject SpawnObject(int spawnablePrefabIndex, uint networkId, int ownerId, Vector3 position, Quaternion rotation)
7988
{
8089
GameObject go = MonoBehaviour.Instantiate(netManager.SpawnablePrefabs[spawnablePrefabIndex]);

0 commit comments

Comments
 (0)