ClientScene.DestroyAllClientObjects moved into NetworkClient

This commit is contained in:
vis2k 2021-03-07 19:28:25 +08:00
parent 27ac635931
commit 477d009f97
2 changed files with 44 additions and 41 deletions

View File

@ -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();
/// <summary>Destroys all networked objects on the client.</summary>
// 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();
}
}

View File

@ -1101,6 +1101,47 @@ internal static void NetworkLateUpdate()
public static void Update() => NetworkLateUpdate();
// shutdown ////////////////////////////////////////////////////////////
/// <summary>Destroys all networked objects on the client.</summary>
// 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.");
}
}
/// <summary>Shutdown the client.</summary>
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.