Skip to content

Commit 8bd712c

Browse files
refactor
Had to make some minor adjustments in order to assure that users could handle sending any last micro-second tasks on any spawned instances prior to them despawning. Added NetworkBehaviour.OnNetworkPreDespawn. Did a slight order of operations on NetworkManager.Shutdown internal in order to assure sending RPCs during despawn would still work. Minor adjustments to the new helper components associated with this PR.
1 parent 13d9a03 commit 8bd712c

File tree

7 files changed

+70
-26
lines changed

7 files changed

+70
-26
lines changed

com.unity.netcode.gameobjects/Runtime/Components/Helpers/AttachableBehaviour.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ internal void InternalDetach()
333333
/// </summary>
334334
public void Detach()
335335
{
336+
if (!gameObject)
337+
{
338+
return;
339+
}
336340
if (!IsSpawned)
337341
{
338342
NetworkLog.LogError($"[{name}][Detach][Not Spawned] Cannot detach if not spawned!");
@@ -399,7 +403,7 @@ private void ChangeReference(NetworkBehaviourReference networkBehaviourReference
399403
}
400404

401405
[Rpc(SendTo.NotMe)]
402-
private void UpdateAttachStateRpc(NetworkBehaviourReference attachedNodeReference)
406+
private void UpdateAttachStateRpc(NetworkBehaviourReference attachedNodeReference, RpcParams rpcParams = default)
403407
{
404408
ChangeReference(attachedNodeReference);
405409
}

com.unity.netcode.gameobjects/Runtime/Components/Helpers/AttachableNode.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public class AttachableNode : NetworkBehaviour
1919
/// </summary>
2020
public bool HasAttachments => m_AttachedBehaviours.Count > 0;
2121

22+
/// <summary>
23+
/// When enabled, any attached <see cref="AttachableBehaviour"/>s will be automatically detached and re-parented under its original parent.
24+
/// </summary>
25+
public bool DetachOnDespawn = true;
26+
2227
/// <summary>
2328
/// A <see cref="List{T}"/> of the currently attached <see cref="AttachableBehaviour"/>s.
2429
/// </summary>
@@ -29,12 +34,22 @@ public class AttachableNode : NetworkBehaviour
2934
/// If the <see cref="NetworkObject"/> this <see cref="AttachableNode"/> belongs to is despawned,
3035
/// then any attached <see cref="AttachableBehaviour"/> will be detached during <see cref="OnNetworkDespawn"/>.
3136
/// </remarks>
32-
public override void OnNetworkDespawn()
37+
public override void OnNetworkPreDespawn()
3338
{
34-
for (int i = m_AttachedBehaviours.Count - 1; i > 0; i--)
39+
if (IsSpawned && HasAuthority && DetachOnDespawn)
3540
{
36-
m_AttachedBehaviours[i].InternalDetach();
41+
for (int i = m_AttachedBehaviours.Count - 1; i >= 0; i--)
42+
{
43+
m_AttachedBehaviours[i]?.Detach();
44+
}
3745
}
46+
base.OnNetworkPreDespawn();
47+
}
48+
49+
/// <inheritdoc/>
50+
public override void OnNetworkDespawn()
51+
{
52+
m_AttachedBehaviours.Clear();
3853
base.OnNetworkDespawn();
3954
}
4055

com.unity.netcode.gameobjects/Runtime/Components/Helpers/ComponentController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ protected virtual bool OnHasAuthority()
462462
}
463463

464464
[Rpc(SendTo.NotMe)]
465-
private void ToggleEnabledRpc(bool enabled)
465+
private void ToggleEnabledRpc(bool enabled, RpcParams rpcParams = default)
466466
{
467467
ChangeEnabled(enabled);
468468
}

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,11 @@ protected virtual void OnInSceneObjectsSpawned() { }
737737
/// </summary>
738738
public virtual void OnNetworkDespawn() { }
739739

740+
/// <summary>
741+
/// Gets called before <see cref="OnNetworkDespawn"/> has been invoked for all <see cref="NetworkBehaviour"/>s associated with the currently spawned <see cref="NetworkObject"/> instance.
742+
/// </summary>
743+
public virtual void OnNetworkPreDespawn() { }
744+
740745
internal void NetworkPreSpawn(ref NetworkManager networkManager)
741746
{
742747
try
@@ -816,6 +821,18 @@ internal void InSceneNetworkObjectsSpawned()
816821
}
817822
}
818823

824+
internal void InternalOnNetworkPreDespawn()
825+
{
826+
try
827+
{
828+
OnNetworkPreDespawn();
829+
}
830+
catch (Exception e)
831+
{
832+
Debug.LogException(e);
833+
}
834+
}
835+
819836
internal void InternalOnNetworkDespawn()
820837
{
821838
IsSpawned = false;

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,12 +1552,27 @@ internal void ShutdownInternal()
15521552
DeferredMessageManager?.CleanupAllTriggers();
15531553
CustomMessagingManager = null;
15541554

1555-
RpcTarget?.Dispose();
1556-
RpcTarget = null;
1557-
15581555
BehaviourUpdater?.Shutdown();
15591556
BehaviourUpdater = null;
15601557

1558+
/// Despawning upon shutdown
1559+
1560+
// We need to clean up NetworkObjects before we reset the IsServer
1561+
// and IsClient properties. This provides consistency of these two
1562+
// property values for NetworkObjects that are still spawned when
1563+
// the shutdown cycle begins.
1564+
1565+
// We need to handle despawning prior to shutting down the connection
1566+
// manager or disposing of the RpcTarget so any final updates can take
1567+
// place (i.e. sending any last state updates or the like).
1568+
1569+
SpawnManager?.DespawnAndDestroyNetworkObjects();
1570+
SpawnManager?.ServerResetShudownStateForSceneObjects();
1571+
////
1572+
1573+
RpcTarget?.Dispose();
1574+
RpcTarget = null;
1575+
15611576
// Shutdown connection manager last which shuts down transport
15621577
ConnectionManager.Shutdown();
15631578

@@ -1567,17 +1582,12 @@ internal void ShutdownInternal()
15671582
MessageManager = null;
15681583
}
15691584

1570-
// We need to clean up NetworkObjects before we reset the IsServer
1571-
// and IsClient properties. This provides consistency of these two
1572-
// property values for NetworkObjects that are still spawned when
1573-
// the shutdown cycle begins.
1574-
SpawnManager?.DespawnAndDestroyNetworkObjects();
1575-
SpawnManager?.ServerResetShudownStateForSceneObjects();
1576-
SpawnManager = null;
1577-
15781585
// Let the NetworkSceneManager clean up its two SceneEvenData instances
15791586
SceneManager?.Dispose();
15801587
SceneManager = null;
1588+
1589+
SpawnManager = null;
1590+
15811591
IsListening = false;
15821592
m_ShuttingDown = false;
15831593

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,6 +2604,12 @@ internal void InternalInSceneNetworkObjectsSpawned()
26042604

26052605
internal void InvokeBehaviourNetworkDespawn()
26062606
{
2607+
// Invoke OnNetworkPreDespawn on all child behaviours
2608+
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
2609+
{
2610+
ChildNetworkBehaviours[i].InternalOnNetworkPreDespawn();
2611+
}
2612+
26072613
NetworkManager.SpawnManager.UpdateOwnershipTable(this, OwnerClientId, true);
26082614
NetworkManager.SpawnManager.RemoveNetworkObjectFromSceneChangedUpdates(this);
26092615

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,16 +1474,8 @@ internal void DespawnAndDestroyNetworkObjects()
14741474
}
14751475
}
14761476

1477-
// If spawned, then despawn and potentially destroy.
1478-
if (networkObjects[i].IsSpawned)
1479-
{
1480-
OnDespawnObject(networkObjects[i], shouldDestroy);
1481-
}
1482-
else // Otherwise, if we are not spawned and we should destroy...then destroy.
1483-
if (shouldDestroy)
1484-
{
1485-
UnityEngine.Object.Destroy(networkObjects[i].gameObject);
1486-
}
1477+
//Despawn and potentially destroy.
1478+
OnDespawnObject(networkObjects[i], shouldDestroy);
14871479
}
14881480
}
14891481
}

0 commit comments

Comments
 (0)