11using System ;
22using System . Collections . Generic ;
3+ using System . Runtime . CompilerServices ;
34
45#if UNITY_EDITOR
56using UnityEditor ;
@@ -261,7 +262,7 @@ internal void ForceDetach()
261262
262263 InternalDetach ( ) ;
263264 // Notify of the changed attached state
264- UpdateAttachState ( m_AttachState , m_AttachableNode ) ;
265+ NotifyAttachedStateChanged ( m_AttachState , m_AttachableNode ) ;
265266
266267 m_AttachedNodeReference = new NetworkBehaviourReference ( null ) ;
267268
@@ -284,42 +285,39 @@ public override void OnNetworkPreDespawn()
284285 base . OnNetworkDespawn ( ) ;
285286 }
286287
288+ /// <summary>
289+ /// This will apply the final attach or detatch state based on the current value of <see cref="m_AttachedNodeReference"/>.
290+ /// </summary>
291+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
287292 private void UpdateAttachedState ( )
288293 {
289- var attachableNode = ( AttachableNode ) null ;
290- var referenceHasNode = m_AttachedNodeReference . TryGet ( out attachableNode , NetworkManager ) ;
294+ // Process the NetworkBehaviourReference to get the new AttachableNode or null.
295+ // If null, then isAttaching will always be false.
296+ var isAttaching = m_AttachedNodeReference . TryGet ( out AttachableNode attachableNode , NetworkManager ) ;
291297
292- /////////////////////////////////////////////////////////////
293298 // Exit early if we are already in the correct attached state and the incoming
294299 // AttachableNode reference is the same as the local AttachableNode property.
295300 if ( attachableNode == m_AttachableNode &&
296- ( ( referenceHasNode && m_AttachState == AttachState . Attached ) ||
297- ( ! referenceHasNode && m_AttachState == AttachState . Detached ) ) )
301+ ( ( isAttaching && m_AttachState == AttachState . Attached ) ||
302+ ( ! isAttaching && m_AttachState == AttachState . Detached ) ) )
298303 {
299304 return ;
300305 }
301306
302- // If we are in an attaching state but the node is null then we are still not attaching.
303- var isAttaching = referenceHasNode && attachableNode != null ;
304-
305307 // If we are attached to some other AttachableNode, then detach from that before attaching to a new one.
306308 if ( isAttaching && m_AttachableNode != null && m_AttachState == AttachState . Attached )
307309 {
308310 // Run through the same process without being triggerd by a NetVar update.
309- UpdateAttachState ( AttachState . Detaching , m_AttachableNode ) ;
311+ NotifyAttachedStateChanged ( AttachState . Detaching , m_AttachableNode ) ;
310312 InternalDetach ( ) ;
311- UpdateAttachState ( AttachState . Detached , m_AttachableNode ) ;
313+ NotifyAttachedStateChanged ( AttachState . Detached , m_AttachableNode ) ;
312314
313315 m_AttachableNode . Detach ( this ) ;
314316 m_AttachableNode = null ;
315317 }
316318
317- // Used for attaching or detatching notifications
318- var preNode = referenceHasNode ? attachableNode : m_AttachableNode ;
319- var preState = referenceHasNode ? AttachState . Attaching : AttachState . Detaching ;
320-
321319 // Change the state to attaching or detaching
322- UpdateAttachState ( preState , preNode ) ;
320+ NotifyAttachedStateChanged ( isAttaching ? AttachState . Attaching : AttachState . Detaching , isAttaching ? attachableNode : m_AttachableNode ) ;
323321
324322 ForceComponentChange ( isAttaching , false ) ;
325323 if ( isAttaching )
@@ -332,7 +330,7 @@ private void UpdateAttachedState()
332330 }
333331
334332 // Notify of the changed attached state
335- UpdateAttachState ( m_AttachState , m_AttachableNode ) ;
333+ NotifyAttachedStateChanged ( m_AttachState , m_AttachableNode ) ;
336334
337335 // When detaching, we want to make our final action
338336 // the invocation of the AttachableNode's Detach method.
@@ -357,7 +355,7 @@ protected virtual void OnAttachStateChanged(AttachState attachState, AttachableN
357355 /// <summary>
358356 /// Update the attached state.
359357 /// </summary>
360- private void UpdateAttachState ( AttachState attachState , AttachableNode attachableNode )
358+ private void NotifyAttachedStateChanged ( AttachState attachState , AttachableNode attachableNode )
361359 {
362360 try
363361 {
@@ -432,7 +430,7 @@ public void Attach(AttachableNode attachableNode)
432430 return ;
433431 }
434432
435- if ( ! HasAuthority )
433+ if ( ! OnHasAuthority ( ) )
436434 {
437435 NetworkLog . LogError ( $ "[{ name } ][Attach][Not Authority] Client-{ NetworkManager . LocalClientId } is not the authority!") ;
438436 return ;
0 commit comments