From 477d009f97ad409c352424fb9608a5fc789dc50d Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 7 Mar 2021 19:28:25 +0800 Subject: [PATCH] ClientScene.DestroyAllClientObjects moved into NetworkClient --- Assets/Mirror/Runtime/ClientScene.cs | 42 ++----------------------- Assets/Mirror/Runtime/NetworkClient.cs | 43 +++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 41 deletions(-) diff --git a/Assets/Mirror/Runtime/ClientScene.cs b/Assets/Mirror/Runtime/ClientScene.cs index 6a05579c7..8d408220f 100644 --- a/Assets/Mirror/Runtime/ClientScene.cs +++ b/Assets/Mirror/Runtime/ClientScene.cs @@ -175,45 +175,7 @@ public static void RegisterSpawnHandler(Guid assetId, SpawnHandlerDelegate spawn [Obsolete("ClientScene.ClearSpawners was moved to NetworkClient.ClearSpawners")] public static void ClearSpawners() => NetworkClient.ClearSpawners(); - /// Destroys all networked objects on the client. - // Note: NetworkServer.CleanupNetworkIdentities does the same on server. - public static void DestroyAllClientObjects() - { - // user can modify spawned lists which causes InvalidOperationException - // list can modified either in UnSpawnHandler or in OnDisable/OnDestroy - // we need the Try/Catch so that the rest of the shutdown does not get stopped - try - { - foreach (NetworkIdentity identity in NetworkIdentity.spawned.Values) - { - if (identity != null && identity.gameObject != null) - { - identity.OnStopClient(); - bool wasUnspawned = NetworkClient.InvokeUnSpawnHandler(identity.assetId, identity.gameObject); - if (!wasUnspawned) - { - // scene objects are reset and disabled. - // they always stay in the scene, we don't destroy them. - if (identity.sceneId != 0) - { - identity.Reset(); - identity.gameObject.SetActive(false); - } - // spawned objects are destroyed - else - { - GameObject.Destroy(identity.gameObject); - } - } - } - } - NetworkIdentity.spawned.Clear(); - } - catch (InvalidOperationException e) - { - Debug.LogException(e); - Debug.LogError("Could not DestroyAllClientObjects because spawned list was modified during loop, make sure you are not modifying NetworkIdentity.spawned by calling NetworkServer.Destroy or NetworkServer.Spawn in OnDestroy or OnDisable."); - } - } + [Obsolete("ClientScene.DestroyAllClientObjects was moved to NetworkClient.DestroyAllClientObjects")] + public static void DestroyAllClientObjects() => NetworkClient.DestroyAllClientObjects(); } } diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 3e6c974d8..200e91090 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -1101,6 +1101,47 @@ internal static void NetworkLateUpdate() public static void Update() => NetworkLateUpdate(); // shutdown //////////////////////////////////////////////////////////// + /// Destroys all networked objects on the client. + // Note: NetworkServer.CleanupNetworkIdentities does the same on server. + public static void DestroyAllClientObjects() + { + // user can modify spawned lists which causes InvalidOperationException + // list can modified either in UnSpawnHandler or in OnDisable/OnDestroy + // we need the Try/Catch so that the rest of the shutdown does not get stopped + try + { + foreach (NetworkIdentity identity in NetworkIdentity.spawned.Values) + { + if (identity != null && identity.gameObject != null) + { + identity.OnStopClient(); + bool wasUnspawned = InvokeUnSpawnHandler(identity.assetId, identity.gameObject); + if (!wasUnspawned) + { + // scene objects are reset and disabled. + // they always stay in the scene, we don't destroy them. + if (identity.sceneId != 0) + { + identity.Reset(); + identity.gameObject.SetActive(false); + } + // spawned objects are destroyed + else + { + GameObject.Destroy(identity.gameObject); + } + } + } + } + NetworkIdentity.spawned.Clear(); + } + catch (InvalidOperationException e) + { + Debug.LogException(e); + Debug.LogError("Could not DestroyAllClientObjects because spawned list was modified during loop, make sure you are not modifying NetworkIdentity.spawned by calling NetworkServer.Destroy or NetworkServer.Spawn in OnDestroy or OnDisable."); + } + } + /// Shutdown the client. public static void Shutdown() { @@ -1110,7 +1151,7 @@ public static void Shutdown() ClientScene.readyConnection = null; ClientScene.ready = false; isSpawnFinished = false; - ClientScene.DestroyAllClientObjects(); + DestroyAllClientObjects(); connectState = ConnectState.None; handlers.Clear(); // disconnect the client connection.