Skip to content

Commit 6daf3ea

Browse files
refactor
Going ahead and adding an (currently) internal "IsDestroying" flag that is set only when the NetworkObject.OnDestroy method is invoked.
1 parent e2e0eee commit 6daf3ea

File tree

4 files changed

+62
-45
lines changed

4 files changed

+62
-45
lines changed

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

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,6 @@ public enum AttachState
205205
private Vector3 m_OriginalLocalPosition;
206206
private Quaternion m_OriginalLocalRotation;
207207

208-
internal bool IsDestroying { get; private set; }
209-
210208
/// <inheritdoc/>
211209
protected override void OnSynchronize<T>(ref BufferSerializer<T> serializer)
212210
{
@@ -240,42 +238,6 @@ protected virtual void Awake()
240238
OnAwake();
241239
}
242240

243-
/// <inheritdoc/>
244-
protected override void OnNetworkPreSpawn(ref NetworkManager networkManager)
245-
{
246-
IsDestroying = false;
247-
// When attached to something else, the attachable needs to know if the
248-
// default parent has been destroyed in order to not attempt to re-parent
249-
// when detached (especially if it is being detached because it should be destroyed).
250-
NetworkObject.OnDestroying += OnDefaultParentDestroying;
251-
252-
base.OnNetworkPreSpawn(ref networkManager);
253-
}
254-
255-
private void OnDefaultParentDestroying()
256-
{
257-
NetworkObject.OnDestroying -= OnDefaultParentDestroying;
258-
// Exit early if we are already being destroyed
259-
if (IsDestroying)
260-
{
261-
return;
262-
}
263-
IsDestroying = true;
264-
// If not completely detached, then destroy the GameObject for
265-
// this attachable since the associated NetworkObject is being
266-
// destroyed.
267-
if (m_AttachState != AttachState.Detached)
268-
{
269-
Destroy(gameObject);
270-
}
271-
}
272-
273-
internal override void InternalOnDestroy()
274-
{
275-
IsDestroying = true;
276-
base.InternalOnDestroy();
277-
}
278-
279241
/// <inheritdoc/>
280242
/// <remarks>
281243
/// If you create a custom <see cref="AttachableBehaviour"/> and override this method, you will want to
@@ -316,6 +278,14 @@ internal void ForceDetach()
316278
/// <inheritdoc/>
317279
public override void OnNetworkPreDespawn()
318280
{
281+
// If the NetworkObject is being destroyed and not completely detached, then destroy the GameObject for
282+
// this attachable since the associated default parent is being destroyed.
283+
if (IsDestroying && m_AttachState != AttachState.Detached)
284+
{
285+
Destroy(gameObject);
286+
return;
287+
}
288+
319289
if (NetworkManager.ShutdownInProgress || AutoDetach.HasFlag(AutoDetachTypes.OnDespawn))
320290
{
321291
ForceDetach();

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public class AttachableNode : NetworkBehaviour
2424
/// </summary>
2525
public bool DetachOnDespawn = true;
2626

27-
internal bool IsDestroying { get; private set; }
28-
2927
/// <summary>
3028
/// A <see cref="List{T}"/> of the currently attached <see cref="AttachableBehaviour"/>s.
3129
/// </summary>
@@ -34,7 +32,6 @@ public class AttachableNode : NetworkBehaviour
3432
/// <inheritdoc/>
3533
protected override void OnNetworkPreSpawn(ref NetworkManager networkManager)
3634
{
37-
IsDestroying = false;
3835
m_AttachedBehaviours.Clear();
3936
base.OnNetworkPreSpawn(ref networkManager);
4037
}
@@ -104,7 +101,6 @@ public override void OnNetworkPreDespawn()
104101

105102
internal override void InternalOnDestroy()
106103
{
107-
IsDestroying = true;
108104
// Notify any attached behaviours that this node is being destroyed.
109105
for (int i = m_AttachedBehaviours.Count - 1; i >= 0; i--)
110106
{

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,20 @@ protected NetworkBehaviour GetNetworkBehaviour(ushort behaviourId)
643643
/// </summary>
644644
public ulong OwnerClientId { get; internal set; }
645645

646+
/// <summary>
647+
/// Returns true if the NetworkObject is in the middle of being destroyed or
648+
/// if there is no valid assigned NetworkObject.
649+
/// </summary>
650+
/// <remarks>
651+
/// <see cref="NetworkObject.IsDestroying"/>
652+
/// </remarks>
653+
internal bool IsDestroying { get; private set; }
654+
655+
internal void SetDestroying()
656+
{
657+
IsDestroying = true;
658+
}
659+
646660
/// <summary>
647661
/// Updates properties with network session related
648662
/// dependencies such as a NetworkObject's spawned

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,13 +1689,50 @@ public static void NetworkHide(List<NetworkObject> networkObjects, ulong clientI
16891689
}
16901690

16911691
/// <summary>
1692-
/// Invoked when the NetworkObject is destroyed.
1692+
/// Returns true if the NetworkObject is in the middle of being destroyed.
16931693
/// </summary>
1694-
internal event Action OnDestroying;
1694+
/// <remarks>
1695+
/// This is particularly useful when determining if something is being de-spawned
1696+
/// normally or if it is being de-spawned because the NetworkObject/GameObject is
1697+
/// being destroyed.
1698+
/// </remarks>
1699+
internal bool IsDestroying { get; private set; }
1700+
1701+
/// <summary>
1702+
/// Applies the despawning flag for the local instance and
1703+
/// its child NetworkBehaviours. Private to assure this is
1704+
/// only invoked from within OnDestroy.
1705+
/// </summary>
1706+
private void SetIsDestroying()
1707+
{
1708+
IsDestroying = true;
1709+
1710+
// Exit early if null
1711+
if (m_ChildNetworkBehaviours == null)
1712+
{
1713+
return;
1714+
}
1715+
1716+
foreach(var childBehaviour in m_ChildNetworkBehaviours)
1717+
{
1718+
// Just ignore and continue processing through the entries
1719+
if (!childBehaviour)
1720+
{
1721+
continue;
1722+
}
1723+
1724+
// Keeping the property a private set to assure this is
1725+
// the only way it can be set as it should never be reset
1726+
// back to false once invoked.
1727+
childBehaviour.SetDestroying();
1728+
}
1729+
}
16951730

16961731
private void OnDestroy()
16971732
{
1698-
OnDestroying?.Invoke();
1733+
// Apply the is destroying flag
1734+
SetIsDestroying();
1735+
16991736
var networkManager = NetworkManager;
17001737
// If no NetworkManager is assigned, then just exit early
17011738
if (!networkManager)

0 commit comments

Comments
 (0)