1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Runtime . CompilerServices ;
3
4
4
5
#if UNITY_EDITOR
5
6
using UnityEditor ;
@@ -261,7 +262,7 @@ internal void ForceDetach()
261
262
262
263
InternalDetach ( ) ;
263
264
// Notify of the changed attached state
264
- UpdateAttachState ( m_AttachState , m_AttachableNode ) ;
265
+ NotifyAttachedStateChanged ( m_AttachState , m_AttachableNode ) ;
265
266
266
267
m_AttachedNodeReference = new NetworkBehaviourReference ( null ) ;
267
268
@@ -284,42 +285,39 @@ public override void OnNetworkPreDespawn()
284
285
base . OnNetworkDespawn ( ) ;
285
286
}
286
287
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 ) ]
287
292
private void UpdateAttachedState ( )
288
293
{
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 ) ;
291
297
292
- /////////////////////////////////////////////////////////////
293
298
// Exit early if we are already in the correct attached state and the incoming
294
299
// AttachableNode reference is the same as the local AttachableNode property.
295
300
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 ) ) )
298
303
{
299
304
return ;
300
305
}
301
306
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
-
305
307
// If we are attached to some other AttachableNode, then detach from that before attaching to a new one.
306
308
if ( isAttaching && m_AttachableNode != null && m_AttachState == AttachState . Attached )
307
309
{
308
310
// Run through the same process without being triggerd by a NetVar update.
309
- UpdateAttachState ( AttachState . Detaching , m_AttachableNode ) ;
311
+ NotifyAttachedStateChanged ( AttachState . Detaching , m_AttachableNode ) ;
310
312
InternalDetach ( ) ;
311
- UpdateAttachState ( AttachState . Detached , m_AttachableNode ) ;
313
+ NotifyAttachedStateChanged ( AttachState . Detached , m_AttachableNode ) ;
312
314
313
315
m_AttachableNode . Detach ( this ) ;
314
316
m_AttachableNode = null ;
315
317
}
316
318
317
- // Used for attaching or detatching notifications
318
- var preNode = referenceHasNode ? attachableNode : m_AttachableNode ;
319
- var preState = referenceHasNode ? AttachState . Attaching : AttachState . Detaching ;
320
-
321
319
// Change the state to attaching or detaching
322
- UpdateAttachState ( preState , preNode ) ;
320
+ NotifyAttachedStateChanged ( isAttaching ? AttachState . Attaching : AttachState . Detaching , isAttaching ? attachableNode : m_AttachableNode ) ;
323
321
324
322
ForceComponentChange ( isAttaching , false ) ;
325
323
if ( isAttaching )
@@ -332,7 +330,7 @@ private void UpdateAttachedState()
332
330
}
333
331
334
332
// Notify of the changed attached state
335
- UpdateAttachState ( m_AttachState , m_AttachableNode ) ;
333
+ NotifyAttachedStateChanged ( m_AttachState , m_AttachableNode ) ;
336
334
337
335
// When detaching, we want to make our final action
338
336
// the invocation of the AttachableNode's Detach method.
@@ -357,7 +355,7 @@ protected virtual void OnAttachStateChanged(AttachState attachState, AttachableN
357
355
/// <summary>
358
356
/// Update the attached state.
359
357
/// </summary>
360
- private void UpdateAttachState ( AttachState attachState , AttachableNode attachableNode )
358
+ private void NotifyAttachedStateChanged ( AttachState attachState , AttachableNode attachableNode )
361
359
{
362
360
try
363
361
{
@@ -432,7 +430,7 @@ public void Attach(AttachableNode attachableNode)
432
430
return ;
433
431
}
434
432
435
- if ( ! HasAuthority )
433
+ if ( ! OnHasAuthority ( ) )
436
434
{
437
435
NetworkLog . LogError ( $ "[{ name } ][Attach][Not Authority] Client-{ NetworkManager . LocalClientId } is not the authority!") ;
438
436
return ;
0 commit comments