Skip to content

Commit efcc404

Browse files
update and test additions
Updating the core components based on some bugs discovered during testing. Updated the AttachableBehaviourTests to validate the different types of auto-detach flag combinations. Fixed some spelling issues with "detatch" (not sure why I got into that habit)...corrected to "detach".
1 parent 42ac2a8 commit efcc404

File tree

3 files changed

+477
-69
lines changed

3 files changed

+477
-69
lines changed

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

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ protected virtual void OnValidate()
9090
// Wait for the next editor update to create a nested child and add the AttachableBehaviour
9191
EditorApplication.update += CreatedNestedChild;
9292
}
93-
93+
if (ComponentControllers == null)
94+
{
95+
return;
96+
}
9497
foreach (var componentController in ComponentControllers)
9598
{
9699
if (componentController == null)
@@ -112,33 +115,33 @@ private void CreatedNestedChild()
112115
}
113116
#endif
114117
/// <summary>
115-
/// Flags to determine if the <see cref="AttachableBehaviour"/> will automatically detatch.
118+
/// Flags to determine if the <see cref="AttachableBehaviour"/> will automatically detach.
116119
/// </summary>
117120
[Flags]
118-
public enum AutoDetatchTypes
121+
public enum AutoDetachTypes
119122
{
120123
/// <summary>
121124
/// Disables auto detach.
122125
/// </summary>
123126
None,
124127
/// <summary>
125-
/// Detatch on ownership change.
128+
/// Detach on ownership change.
126129
/// </summary>
127130
OnOwnershipChange,
128131
/// <summary>
129-
/// Detatch on despawn.
132+
/// Detach on despawn.
130133
/// </summary>
131134
OnDespawn,
132135
/// <summary>
133-
/// Detatch on destroy.
136+
/// Detach on destroy.
134137
/// </summary>
135138
OnAttachNodeDestroy,
136139
}
137140

138141
/// <summary>
139-
/// Determines if this <see cref="AttachableBehaviour"/> will automatically detatch on all instances if it has one of the <see cref="AutoDetatchTypes"/> flags.
142+
/// Determines if this <see cref="AttachableBehaviour"/> will automatically detach on all instances if it has one of the <see cref="AutoDetachTypes"/> flags.
140143
/// </summary>
141-
public AutoDetatchTypes AutoDetach = AutoDetatchTypes.OnDespawn | AutoDetatchTypes.OnOwnershipChange | AutoDetatchTypes.OnAttachNodeDestroy;
144+
public AutoDetachTypes AutoDetach = AutoDetachTypes.OnDespawn | AutoDetachTypes.OnOwnershipChange | AutoDetachTypes.OnAttachNodeDestroy;
142145

143146
[SerializeField]
144147
internal List<ComponentControllerEntry> ComponentControllers;
@@ -252,29 +255,36 @@ protected override void OnNetworkSessionSynchronized()
252255
base.OnNetworkSessionSynchronized();
253256
}
254257

255-
internal void ForceDetatch()
258+
internal void ForceDetach()
256259
{
257260
if (m_AttachState == AttachState.Detached || m_AttachState == AttachState.Detaching)
258261
{
259262
return;
260263
}
261264

262265
ForceComponentChange(false, true);
266+
263267
InternalDetach();
264-
if (NetworkManager && !NetworkManager.ShutdownInProgress)
268+
// Notify of the changed attached state
269+
UpdateAttachState(m_AttachState, m_AttachableNode);
270+
271+
m_AttachedNodeReference = new NetworkBehaviourReference(null);
272+
273+
// When detaching, we want to make our final action
274+
// the invocation of the AttachableNode's Detach method.
275+
if (m_AttachableNode)
265276
{
266-
// Notify of the changed attached state
267-
UpdateAttachState(m_AttachState, m_AttachableNode);
277+
m_AttachableNode.Detach(this);
278+
m_AttachableNode = null;
268279
}
269-
m_AttachedNodeReference = new NetworkBehaviourReference(null);
270280
}
271281

272282
/// <inheritdoc/>
273283
public override void OnNetworkPreDespawn()
274284
{
275-
if (AutoDetach.HasFlag(AutoDetatchTypes.OnDespawn))
285+
if (AutoDetach.HasFlag(AutoDetachTypes.OnDespawn))
276286
{
277-
ForceDetatch();
287+
ForceDetach();
278288
}
279289
base.OnNetworkDespawn();
280290
}
@@ -375,9 +385,9 @@ private void UpdateAttachState(AttachState attachState, AttachableNode attachabl
375385
/// <inheritdoc/>
376386
protected override void OnOwnershipChanged(ulong previous, ulong current)
377387
{
378-
if (AutoDetach.HasFlag(AutoDetatchTypes.OnOwnershipChange))
388+
if (AutoDetach.HasFlag(AutoDetachTypes.OnOwnershipChange))
379389
{
380-
ForceDetatch();
390+
ForceDetach();
381391
}
382392
base.OnOwnershipChanged(previous, current);
383393
}
@@ -551,11 +561,11 @@ private void UpdateAttachStateRpc(NetworkBehaviourReference attachedNodeReferenc
551561
/// </summary>
552562
internal void OnAttachNodeDestroy()
553563
{
554-
// If this instance should force a detatch on destroy
555-
if (AutoDetach.HasFlag(AutoDetatchTypes.OnAttachNodeDestroy))
564+
// If this instance should force a detach on destroy
565+
if (AutoDetach.HasFlag(AutoDetachTypes.OnAttachNodeDestroy))
556566
{
557-
// Force a detatch
558-
ForceDetatch();
567+
// Force a detach
568+
ForceDetach();
559569
}
560570
}
561571
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ protected override void OnNetworkPreSpawn(ref NetworkManager networkManager)
4343
/// </remarks>
4444
protected override void OnOwnershipChanged(ulong previous, ulong current)
4545
{
46+
// Clear any known behaviours on all instances (really only the previous owner should know about AttachedBehaviours
47+
m_AttachedBehaviours.Clear();
4648
if (current == NetworkManager.LocalClientId)
4749
{
48-
m_AttachedBehaviours.Clear();
50+
// Rebuild the list of AttachableBehaviours for the new owner
4951
var attachables = NetworkObject.transform.GetComponentsInChildren<AttachableBehaviour>();
5052
foreach (var attachable in attachables)
5153
{
@@ -69,7 +71,21 @@ public override void OnNetworkPreDespawn()
6971
{
7072
for (int i = m_AttachedBehaviours.Count - 1; i >= 0; i--)
7173
{
72-
m_AttachedBehaviours[i]?.Detach();
74+
if (!m_AttachedBehaviours[i])
75+
{
76+
continue;
77+
}
78+
// If we don't have authority but should detach on despawn,
79+
// then proceed to detach.
80+
if (!m_AttachedBehaviours[i].HasAuthority)
81+
{
82+
m_AttachedBehaviours[i].ForceDetach();
83+
}
84+
else
85+
{
86+
// Detach the normal way with authority
87+
m_AttachedBehaviours[i].Detach();
88+
}
7389
}
7490
}
7591
base.OnNetworkPreDespawn();

0 commit comments

Comments
 (0)