@@ -64,45 +64,53 @@ public void ChangeOwnership(int newOwnerClientId)
6464
6565 internal void InvokeBehaviourOnLostOwnership ( )
6666 {
67- NetworkedBehaviour [ ] netBehaviours = GetComponentsInChildren < NetworkedBehaviour > ( ) ;
68- for ( int i = 0 ; i < netBehaviours . Length ; i ++ )
67+ for ( int i = 0 ; i < childNetworkedBehaviours . Count ; i ++ )
6968 {
70- //We check if we are it's networkedObject owner incase a networkedObject exists as a child of our networkedObject.
71- if ( netBehaviours [ i ] . networkedObject == this )
72- {
73- netBehaviours [ i ] . OnLostOwnership ( ) ;
74- }
69+ childNetworkedBehaviours [ i ] . OnLostOwnership ( ) ;
7570 }
7671 }
7772
7873 internal void InvokeBehaviourOnGainedOwnership ( )
7974 {
80- NetworkedBehaviour [ ] netBehaviours = GetComponentsInChildren < NetworkedBehaviour > ( ) ;
81- for ( int i = 0 ; i < netBehaviours . Length ; i ++ )
75+ for ( int i = 0 ; i < childNetworkedBehaviours . Count ; i ++ )
8276 {
83- //We check if we are it's networkedObject owner incase a networkedObject exists as a child of our networkedObject.
84- if ( netBehaviours [ i ] . networkedObject == this )
85- {
86- netBehaviours [ i ] . OnGainedOwnership ( ) ;
87- }
77+ childNetworkedBehaviours [ i ] . OnGainedOwnership ( ) ;
8878 }
8979 }
9080
9181 internal void InvokeBehaviourNetworkSpawn ( )
9282 {
93- NetworkedBehaviour [ ] netBehaviours = GetComponentsInChildren < NetworkedBehaviour > ( ) ;
94- for ( int i = 0 ; i < netBehaviours . Length ; i ++ )
83+ for ( int i = 0 ; i < childNetworkedBehaviours . Count ; i ++ )
9584 {
9685 //We check if we are it's networkedObject owner incase a networkedObject exists as a child of our networkedObject.
97- if ( netBehaviours [ i ] . networkedObject == this && ! netBehaviours [ i ] . networkedStartInvoked )
86+ if ( ! childNetworkedBehaviours [ i ] . networkedStartInvoked )
9887 {
99- netBehaviours [ i ] . NetworkStart ( ) ;
88+ childNetworkedBehaviours [ i ] . NetworkStart ( ) ;
10089 if ( NetworkingManager . singleton . isServer )
101- netBehaviours [ i ] . SyncVarInit ( ) ;
90+ childNetworkedBehaviours [ i ] . SyncVarInit ( ) ;
10291 }
10392 }
10493 }
10594
95+ private List < NetworkedBehaviour > _childNetworkedBehaviours ;
96+ internal List < NetworkedBehaviour > childNetworkedBehaviours
97+ {
98+ get
99+ {
100+ if ( _childNetworkedBehaviours == null )
101+ {
102+ _childNetworkedBehaviours = new List < NetworkedBehaviour > ( ) ;
103+ NetworkedBehaviour [ ] behaviours = GetComponentsInChildren < NetworkedBehaviour > ( ) ;
104+ for ( int i = 0 ; i < behaviours . Length ; i ++ )
105+ {
106+ if ( behaviours [ i ] . networkedObject == this )
107+ _childNetworkedBehaviours . Add ( behaviours [ i ] ) ;
108+ }
109+ }
110+ return _childNetworkedBehaviours ;
111+ }
112+ }
113+
106114 internal static List < NetworkedBehaviour > NetworkedBehaviours = new List < NetworkedBehaviour > ( ) ;
107115 internal static void InvokeSyncvarUpdate ( )
108116 {
@@ -115,27 +123,26 @@ internal static void InvokeSyncvarUpdate()
115123 //Flushes all syncVars to client
116124 internal void FlushToClient ( int clientId )
117125 {
118- for ( int i = 0 ; i < NetworkedBehaviours . Count ; i ++ )
126+ for ( int i = 0 ; i < childNetworkedBehaviours . Count ; i ++ )
119127 {
120- NetworkedBehaviours [ i ] . FlushToClient ( clientId ) ;
128+ childNetworkedBehaviours [ i ] . FlushToClient ( clientId ) ;
121129 }
122130 }
123131
124132 internal ushort GetOrderIndex ( NetworkedBehaviour instance )
125133 {
126- NetworkedBehaviour [ ] behaviours = GetComponentsInChildren < NetworkedBehaviour > ( ) ;
127- for ( ushort i = 0 ; i < behaviours . Length ; i ++ )
134+ for ( ushort i = 0 ; i < childNetworkedBehaviours . Count ; i ++ )
128135 {
129- if ( behaviours [ i ] . networkedObject == this && behaviours [ i ] == instance )
136+ if ( childNetworkedBehaviours [ i ] == instance )
130137 return i ;
131138 }
132139 return 0 ;
133140 }
134141
135142 internal NetworkedBehaviour GetBehaviourAtOrderIndex ( ushort index )
136143 {
137- NetworkedBehaviour [ ] behaviours = GetComponentsInChildren < NetworkedBehaviour > ( ) ;
138- return behaviours [ index ] ;
144+ //TODO index out of bounds
145+ return childNetworkedBehaviours [ index ] ;
139146 }
140147 }
141148}
0 commit comments