@@ -1119,7 +1119,8 @@ private bool InternalHasAuthority()
11191119 }
11201120
11211121 /// <summary>
1122- /// The NetworkManager that is responsible for this NetworkObject instance.
1122+ /// The NetworkManager that owns this NetworkObject.
1123+ /// This property controls where this NetworkObject belongs.
11231124 /// This property is null by default currently, which means that the above NetworkManager getter will return the Singleton.
11241125 /// In the future this is the path where alternative NetworkManagers should be injected for running multi NetworkManagers
11251126 /// </summary>
@@ -1770,9 +1771,6 @@ internal void SpawnInternal(bool destroyWithScene, ulong ownerClientId, bool pla
17701771 {
17711772 if ( NetworkManagerOwner == null )
17721773 {
1773- #if TEST_NO_SINGLETON
1774- Debug . LogError ( "NetworkObject has no owner client! setting as singleton owner" ) ;
1775- #endif
17761774 NetworkManagerOwner = NetworkManager . Singleton ;
17771775 }
17781776 if ( ! NetworkManager . IsListening )
@@ -1929,7 +1927,7 @@ public NetworkObject InstantiateAndSpawn(NetworkManager networkManager, ulong ow
19291927 return null ;
19301928 }
19311929
1932- return networkManager . SpawnManager . InstantiateAndSpawnNoParameterChecks ( this , ownerClientId , destroyWithScene , isPlayerObject , forceOverride , position , rotation , networkManager ) ;
1930+ return networkManager . SpawnManager . InstantiateAndSpawnNoParameterChecks ( this , ownerClientId , destroyWithScene , isPlayerObject , forceOverride , position , rotation ) ;
19331931 }
19341932
19351933 /// <summary>
@@ -2060,13 +2058,6 @@ internal void InvokeOwnershipChanged(ulong previous, ulong next)
20602058
20612059 internal void InvokeBehaviourOnNetworkObjectParentChanged ( NetworkObject parentNetworkObject )
20622060 {
2063- if ( NetworkManagerOwner == null )
2064- {
2065- #if TEST_NO_SINGLETON
2066- Debug . LogError ( "NetworkManagerOwner should be set! Setting owner to NetworkManager.Singleton" ) ;
2067- #endif
2068- NetworkManagerOwner = NetworkManager . Singleton ;
2069- }
20702061 for ( int i = 0 ; i < ChildNetworkBehaviours . Count ; i ++ )
20712062 {
20722063 // Invoke internal notification
@@ -2295,13 +2286,6 @@ private void OnTransformParentChanged()
22952286
22962287 if ( ! IsSpawned )
22972288 {
2298- if ( NetworkManagerOwner == null )
2299- {
2300- #if TEST_NO_SINGLETON
2301- Debug . LogError ( "NetworkManagerOwner should be set! Setting owner to NetworkManager.Singleton" ) ;
2302- #endif
2303- NetworkManagerOwner = NetworkManager ;
2304- }
23052289 AuthorityAppliedParenting = false ;
23062290 // and we are removing the parent, then go ahead and allow parenting to occur
23072291 if ( transform . parent == null )
@@ -2550,7 +2534,7 @@ internal static void CheckOrphanChildren()
25502534
25512535 internal void InvokeBehaviourNetworkPreSpawn ( )
25522536 {
2553- var networkManager = NetworkManagerOwner ;
2537+ var networkManager = NetworkManager ;
25542538 for ( int i = 0 ; i < ChildNetworkBehaviours . Count ; i ++ )
25552539 {
25562540 if ( ChildNetworkBehaviours [ i ] . gameObject . activeInHierarchy )
@@ -2633,68 +2617,53 @@ internal List<NetworkBehaviour> ChildNetworkBehaviours
26332617 {
26342618 get
26352619 {
2636- if ( m_ChildNetworkBehaviours = = null )
2620+ if ( m_ChildNetworkBehaviours ! = null )
26372621 {
2638- m_ChildNetworkBehaviours = BuildChildBehavioursList ( ) ;
2622+ return m_ChildNetworkBehaviours ;
26392623 }
26402624
2641- return m_ChildNetworkBehaviours ;
2642- }
2643- }
2644-
2645- private List < NetworkBehaviour > BuildChildBehavioursList ( )
2646- {
2647- if ( NetworkManagerOwner == null )
2648- {
2649- #if TEST_NO_SINGLETON
2650- Debug . LogError ( "NetworkManagerOwner should be set! Setting owner to NetworkManager.Singleton" ) ;
2651- #endif
2652- NetworkManagerOwner = NetworkManager . Singleton ;
2653- }
2654-
2655- var networkBehaviours = GetComponentsInChildren < NetworkBehaviour > ( true ) ;
2656- var childBehaviours = new List < NetworkBehaviour > ( networkBehaviours . Length ) ;
2657-
2658- foreach ( var behaviour in networkBehaviours )
2659- {
2660- // Find the first parent NetworkObject of this child
2661- // if it's not ourselves, this childBehaviour belongs to a different NetworkObject.
2662- var networkObj = behaviour . GetComponentInParent < NetworkObject > ( ) ;
2663- if ( networkObj != this )
2625+ m_ChildNetworkBehaviours = new List < NetworkBehaviour > ( ) ;
2626+ var networkBehaviours = GetComponentsInChildren < NetworkBehaviour > ( true ) ;
2627+ for ( int i = 0 ; i < networkBehaviours . Length ; i ++ )
26642628 {
2665- continue ;
2666- }
2629+ // Find the first parent NetworkObject of this child
2630+ // if it's not ourselves, this childBehaviour belongs to a different NetworkObject.
2631+ var networkObj = networkBehaviours [ i ] . GetComponentInParent < NetworkObject > ( ) ;
2632+ if ( networkObj != this )
2633+ {
2634+ continue ;
2635+ }
26672636
2668- // Set ourselves as the NetworkObject that this behaviour belongs to and add it to the child list
2669- var nextIndex = childBehaviours . Count ;
2670- childBehaviours . Add ( behaviour ) ;
2671- behaviour . SetNetworkObject ( this , ( ushort ) nextIndex ) ;
2637+ // Set ourselves as the NetworkObject that this behaviour belongs to and add it to the child list
2638+ var nextIndex = ( ushort ) m_ChildNetworkBehaviours . Count ;
2639+ networkBehaviours [ i ] . SetNetworkObject ( this , nextIndex ) ;
2640+ m_ChildNetworkBehaviours . Add ( networkBehaviours [ i ] ) ;
26722641
2673- var type = behaviour . GetType ( ) ;
2674- if ( type == typeof ( NetworkTransform ) || type . IsAssignableFrom ( typeof ( NetworkTransform ) ) || type . IsSubclassOf ( typeof ( NetworkTransform ) ) )
2675- {
2676- if ( NetworkTransforms == null )
2642+ var type = networkBehaviours [ i ] . GetType ( ) ;
2643+ if ( type == typeof ( NetworkTransform ) || type . IsInstanceOfType ( typeof ( NetworkTransform ) ) || type . IsSubclassOf ( typeof ( NetworkTransform ) ) )
26772644 {
2678- NetworkTransforms = new List < NetworkTransform > ( ) ;
2645+ if ( NetworkTransforms == null )
2646+ {
2647+ NetworkTransforms = new List < NetworkTransform > ( ) ;
2648+ }
2649+ var networkTransform = networkBehaviours [ i ] as NetworkTransform ;
2650+ networkTransform . IsNested = i != 0 && networkTransform . gameObject != gameObject ;
2651+ NetworkTransforms . Add ( networkTransform ) ;
26792652 }
2680- var networkTransform = behaviour as NetworkTransform ;
2681- networkTransform . IsNested = networkTransform . gameObject != gameObject ;
2682- NetworkTransforms . Add ( networkTransform ) ;
2683- }
26842653#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
2685- else if ( type . IsSubclassOf ( typeof ( NetworkRigidbodyBase ) ) )
2686- {
2687- if ( NetworkRigidbodies == null )
2654+ else if ( type . IsSubclassOf ( typeof ( NetworkRigidbodyBase ) ) )
26882655 {
2689- NetworkRigidbodies = new List < NetworkRigidbodyBase > ( ) ;
2656+ if ( NetworkRigidbodies == null )
2657+ {
2658+ NetworkRigidbodies = new List < NetworkRigidbodyBase > ( ) ;
2659+ }
2660+ NetworkRigidbodies . Add ( networkBehaviours [ i ] as NetworkRigidbodyBase ) ;
26902661 }
2691- NetworkRigidbodies . Add ( behaviour as NetworkRigidbodyBase ) ;
2692- }
26932662#endif
2694- }
2663+ }
26952664
2696- childBehaviours . TrimExcess ( ) ;
2697- return childBehaviours ;
2665+ return m_ChildNetworkBehaviours ;
2666+ }
26982667 }
26992668
27002669 /// <summary>
@@ -2775,24 +2744,26 @@ internal static void VerifyParentingStatus()
27752744 /// </returns>
27762745 public ushort GetNetworkBehaviourOrderIndex ( NetworkBehaviour instance )
27772746 {
2778- if ( ! IsSpawned )
2747+ // read the cached index, and verify it first
2748+ if ( instance . NetworkBehaviourIdCache < ChildNetworkBehaviours . Count )
27792749 {
2780- if ( NetworkLog . CurrentLogLevel <= LogLevel . Developer )
2750+ if ( ChildNetworkBehaviours [ instance . NetworkBehaviourIdCache ] == instance )
27812751 {
2782- Debug . LogWarning ( $ " { nameof ( NetworkObject ) } is not spawned yet. Cannot get index of NetworkBehaviour." ) ;
2752+ return instance . NetworkBehaviourIdCache ;
27832753 }
2784- return 0 ;
2754+
2755+ // invalid cached id reset
2756+ instance . NetworkBehaviourIdCache = default ;
27852757 }
27862758
2787- // read the cached index, and verify it first
2788- if ( instance . NetworkBehaviourId < ChildNetworkBehaviours . Count )
2759+ for ( ushort i = 0 ; i < ChildNetworkBehaviours . Count ; i ++ )
27892760 {
2790- if ( ChildNetworkBehaviours [ instance . NetworkBehaviourId ] == instance )
2761+ if ( ChildNetworkBehaviours [ i ] == instance )
27912762 {
2792- return instance . NetworkBehaviourId ;
2763+ // cache the id, for next query
2764+ instance . NetworkBehaviourIdCache = i ;
2765+ return i ;
27932766 }
2794-
2795- Debug . LogError ( "Network behaviour at index has changed. This should not be possible." ) ;
27962767 }
27972768
27982769 return 0 ;
@@ -2805,15 +2776,6 @@ public ushort GetNetworkBehaviourOrderIndex(NetworkBehaviour instance)
28052776 /// <returns>The <see cref="NetworkBehaviour"/> at the ordered index value or null if it does not exist.</returns>
28062777 public NetworkBehaviour GetNetworkBehaviourAtOrderIndex ( ushort index )
28072778 {
2808- if ( ! IsSpawned )
2809- {
2810- if ( NetworkLog . CurrentLogLevel <= LogLevel . Developer )
2811- {
2812- Debug . LogWarning ( $ "{ nameof ( NetworkObject ) } is not spawned yet. Cannot get NetworkBehaviour at index.") ;
2813- }
2814- return null ;
2815- }
2816-
28172779 if ( index >= ChildNetworkBehaviours . Count )
28182780 {
28192781 if ( NetworkLog . CurrentLogLevel <= LogLevel . Error )
0 commit comments