@@ -1119,8 +1119,7 @@ private bool InternalHasAuthority()
11191119 }
11201120
11211121 /// <summary>
1122- /// The NetworkManager that owns this NetworkObject.
1123- /// This property controls where this NetworkObject belongs.
1122+ /// The NetworkManager that is responsible for this NetworkObject instance.
11241123 /// This property is null by default currently, which means that the above NetworkManager getter will return the Singleton.
11251124 /// In the future this is the path where alternative NetworkManagers should be injected for running multi NetworkManagers
11261125 /// </summary>
@@ -1771,6 +1770,9 @@ internal void SpawnInternal(bool destroyWithScene, ulong ownerClientId, bool pla
17711770 {
17721771 if ( NetworkManagerOwner == null )
17731772 {
1773+ #if TEST_NO_SINGLETON
1774+ Debug . LogError ( "NetworkObject has no owner client! setting as singleton owner" ) ;
1775+ #endif
17741776 NetworkManagerOwner = NetworkManager . Singleton ;
17751777 }
17761778 if ( ! NetworkManager . IsListening )
@@ -1825,7 +1827,7 @@ internal void SpawnInternal(bool destroyWithScene, ulong ownerClientId, bool pla
18251827 }
18261828 }
18271829
1828- NetworkManager . SpawnManager . SpawnNetworkObjectLocally ( this , NetworkManager . SpawnManager . GetNetworkObjectId ( ) , IsSceneObject . HasValue && IsSceneObject . Value , playerObject , ownerClientId , destroyWithScene ) ;
1830+ NetworkManager . SpawnManager . SpawnNetworkObjectLocally ( this , NetworkManagerOwner , NetworkManager . SpawnManager . GetNetworkObjectId ( ) , IsSceneObject . HasValue && IsSceneObject . Value , playerObject , ownerClientId , destroyWithScene ) ;
18291831
18301832 if ( ( NetworkManager . DistributedAuthorityMode && NetworkManager . DAHost ) || ( ! NetworkManager . DistributedAuthorityMode && NetworkManager . IsServer ) )
18311833 {
@@ -2534,7 +2536,7 @@ internal static void CheckOrphanChildren()
25342536
25352537 internal void InvokeBehaviourNetworkPreSpawn ( )
25362538 {
2537- var networkManager = NetworkManager ;
2539+ var networkManager = NetworkManagerOwner ;
25382540 for ( int i = 0 ; i < ChildNetworkBehaviours . Count ; i ++ )
25392541 {
25402542 if ( ChildNetworkBehaviours [ i ] . gameObject . activeInHierarchy )
@@ -2611,58 +2613,74 @@ internal void InvokeBehaviourNetworkDespawn()
26112613 }
26122614 }
26132615
2614- private List < NetworkBehaviour > m_ChildNetworkBehaviours ;
2616+ internal List < NetworkBehaviour > m_ChildNetworkBehaviours ;
26152617
26162618 internal List < NetworkBehaviour > ChildNetworkBehaviours
26172619 {
26182620 get
26192621 {
2620- if ( m_ChildNetworkBehaviours ! = null )
2622+ if ( m_ChildNetworkBehaviours = = null )
26212623 {
2622- return m_ChildNetworkBehaviours ;
2624+ m_ChildNetworkBehaviours = BuildChildBehavioursList ( ) ;
26232625 }
26242626
2625- m_ChildNetworkBehaviours = new List < NetworkBehaviour > ( ) ;
2626- var networkBehaviours = GetComponentsInChildren < NetworkBehaviour > ( true ) ;
2627- for ( int i = 0 ; i < networkBehaviours . Length ; i ++ )
2627+ return m_ChildNetworkBehaviours ;
2628+ }
2629+ }
2630+
2631+ private List < NetworkBehaviour > BuildChildBehavioursList ( )
2632+ {
2633+ #if UNITY_EDITOR
2634+ if ( NetworkManagerOwner == null )
2635+ {
2636+ Debug . LogError ( "NetworkManagerOwner should be set! Setting owner to NetworkManager.Singleton" ) ;
2637+ NetworkManagerOwner = NetworkManager . Singleton ;
2638+ }
2639+ #endif
2640+
2641+ var networkBehaviours = GetComponentsInChildren < NetworkBehaviour > ( true ) ;
2642+ var childBehaviours = new List < NetworkBehaviour > ( networkBehaviours . Length ) ;
2643+
2644+ foreach ( var behaviour in networkBehaviours )
2645+ {
2646+ // Find the first parent NetworkObject of this child
2647+ // if it's not ourselves, this childBehaviour belongs to a different NetworkObject.
2648+ var networkObj = behaviour . GetComponentInParent < NetworkObject > ( ) ;
2649+ if ( networkObj != this )
26282650 {
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- }
2651+ continue ;
2652+ }
26362653
2637- // Set ourselves as the NetworkObject that this behaviour belongs to and add it to the child list
2638- networkBehaviours [ i ] . SetNetworkObject ( this ) ;
2639- m_ChildNetworkBehaviours . Add ( networkBehaviours [ i ] ) ;
2654+ // Set ourselves as the NetworkObject that this behaviour belongs to and add it to the child list
2655+ var nextIndex = childBehaviours . Count ;
2656+ childBehaviours . Add ( behaviour ) ;
2657+ behaviour . SetNetworkObject ( this , ( ushort ) nextIndex ) ;
26402658
2641- var type = networkBehaviours [ i ] . GetType ( ) ;
2642- if ( type == typeof ( NetworkTransform ) || type . IsInstanceOfType ( typeof ( NetworkTransform ) ) || type . IsSubclassOf ( typeof ( NetworkTransform ) ) )
2659+ var type = behaviour . GetType ( ) ;
2660+ if ( type == typeof ( NetworkTransform ) || type . IsAssignableFrom ( typeof ( NetworkTransform ) ) || type . IsSubclassOf ( typeof ( NetworkTransform ) ) )
2661+ {
2662+ if ( NetworkTransforms == null )
26432663 {
2644- if ( NetworkTransforms == null )
2645- {
2646- NetworkTransforms = new List < NetworkTransform > ( ) ;
2647- }
2648- var networkTransform = networkBehaviours [ i ] as NetworkTransform ;
2649- networkTransform . IsNested = i != 0 && networkTransform . gameObject != gameObject ;
2650- NetworkTransforms . Add ( networkTransform ) ;
2664+ NetworkTransforms = new List < NetworkTransform > ( ) ;
26512665 }
2666+ var networkTransform = behaviour as NetworkTransform ;
2667+ networkTransform . IsNested = networkTransform . gameObject != gameObject ;
2668+ NetworkTransforms . Add ( networkTransform ) ;
2669+ }
26522670#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
2653- else if ( type . IsSubclassOf ( typeof ( NetworkRigidbodyBase ) ) )
2671+ else if ( type . IsSubclassOf ( typeof ( NetworkRigidbodyBase ) ) )
2672+ {
2673+ if ( NetworkRigidbodies == null )
26542674 {
2655- if ( NetworkRigidbodies == null )
2656- {
2657- NetworkRigidbodies = new List < NetworkRigidbodyBase > ( ) ;
2658- }
2659- NetworkRigidbodies . Add ( networkBehaviours [ i ] as NetworkRigidbodyBase ) ;
2675+ NetworkRigidbodies = new List < NetworkRigidbodyBase > ( ) ;
26602676 }
2661- #endif
2677+ NetworkRigidbodies . Add ( behaviour as NetworkRigidbodyBase ) ;
26622678 }
2663-
2664- return m_ChildNetworkBehaviours ;
2679+ #endif
26652680 }
2681+
2682+ childBehaviours . TrimExcess ( ) ;
2683+ return childBehaviours ;
26662684 }
26672685
26682686 /// <summary>
@@ -2744,25 +2762,14 @@ internal static void VerifyParentingStatus()
27442762 public ushort GetNetworkBehaviourOrderIndex ( NetworkBehaviour instance )
27452763 {
27462764 // read the cached index, and verify it first
2747- if ( instance . NetworkBehaviourIdCache < ChildNetworkBehaviours . Count )
2765+ if ( instance . NetworkBehaviourId < ChildNetworkBehaviours . Count )
27482766 {
2749- if ( ChildNetworkBehaviours [ instance . NetworkBehaviourIdCache ] == instance )
2767+ if ( ChildNetworkBehaviours [ instance . NetworkBehaviourId ] == instance )
27502768 {
2751- return instance . NetworkBehaviourIdCache ;
2769+ return instance . NetworkBehaviourId ;
27522770 }
27532771
2754- // invalid cached id reset
2755- instance . NetworkBehaviourIdCache = default ;
2756- }
2757-
2758- for ( ushort i = 0 ; i < ChildNetworkBehaviours . Count ; i ++ )
2759- {
2760- if ( ChildNetworkBehaviours [ i ] == instance )
2761- {
2762- // cache the id, for next query
2763- instance . NetworkBehaviourIdCache = i ;
2764- return i ;
2765- }
2772+ Debug . LogError ( "Network behaviour at index has changed. This should not be possible." ) ;
27662773 }
27672774
27682775 return 0 ;
@@ -3246,6 +3253,8 @@ internal static NetworkObject AddSceneObject(in SceneObject sceneObject, FastBuf
32463253 return null ;
32473254 }
32483255
3256+ networkObject . NetworkManagerOwner = networkManager ;
3257+
32493258 // This will get set again when the NetworkObject is spawned locally, but we set it here ahead of spawning
32503259 // in order to be able to determine which NetworkVariables the client will be allowed to read.
32513260 networkObject . OwnerClientId = sceneObject . OwnerClientId ;
0 commit comments