@@ -64,45 +64,53 @@ public void ChangeOwnership(int newOwnerClientId)
64
64
65
65
internal void InvokeBehaviourOnLostOwnership ( )
66
66
{
67
- NetworkedBehaviour [ ] netBehaviours = GetComponentsInChildren < NetworkedBehaviour > ( ) ;
68
- for ( int i = 0 ; i < netBehaviours . Length ; i ++ )
67
+ for ( int i = 0 ; i < childNetworkedBehaviours . Count ; i ++ )
69
68
{
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 ( ) ;
75
70
}
76
71
}
77
72
78
73
internal void InvokeBehaviourOnGainedOwnership ( )
79
74
{
80
- NetworkedBehaviour [ ] netBehaviours = GetComponentsInChildren < NetworkedBehaviour > ( ) ;
81
- for ( int i = 0 ; i < netBehaviours . Length ; i ++ )
75
+ for ( int i = 0 ; i < childNetworkedBehaviours . Count ; i ++ )
82
76
{
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 ( ) ;
88
78
}
89
79
}
90
80
91
81
internal void InvokeBehaviourNetworkSpawn ( )
92
82
{
93
- NetworkedBehaviour [ ] netBehaviours = GetComponentsInChildren < NetworkedBehaviour > ( ) ;
94
- for ( int i = 0 ; i < netBehaviours . Length ; i ++ )
83
+ for ( int i = 0 ; i < childNetworkedBehaviours . Count ; i ++ )
95
84
{
96
85
//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 )
98
87
{
99
- netBehaviours [ i ] . NetworkStart ( ) ;
88
+ childNetworkedBehaviours [ i ] . NetworkStart ( ) ;
100
89
if ( NetworkingManager . singleton . isServer )
101
- netBehaviours [ i ] . SyncVarInit ( ) ;
90
+ childNetworkedBehaviours [ i ] . SyncVarInit ( ) ;
102
91
}
103
92
}
104
93
}
105
94
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
+
106
114
internal static List < NetworkedBehaviour > NetworkedBehaviours = new List < NetworkedBehaviour > ( ) ;
107
115
internal static void InvokeSyncvarUpdate ( )
108
116
{
@@ -115,27 +123,26 @@ internal static void InvokeSyncvarUpdate()
115
123
//Flushes all syncVars to client
116
124
internal void FlushToClient ( int clientId )
117
125
{
118
- for ( int i = 0 ; i < NetworkedBehaviours . Count ; i ++ )
126
+ for ( int i = 0 ; i < childNetworkedBehaviours . Count ; i ++ )
119
127
{
120
- NetworkedBehaviours [ i ] . FlushToClient ( clientId ) ;
128
+ childNetworkedBehaviours [ i ] . FlushToClient ( clientId ) ;
121
129
}
122
130
}
123
131
124
132
internal ushort GetOrderIndex ( NetworkedBehaviour instance )
125
133
{
126
- NetworkedBehaviour [ ] behaviours = GetComponentsInChildren < NetworkedBehaviour > ( ) ;
127
- for ( ushort i = 0 ; i < behaviours . Length ; i ++ )
134
+ for ( ushort i = 0 ; i < childNetworkedBehaviours . Count ; i ++ )
128
135
{
129
- if ( behaviours [ i ] . networkedObject == this && behaviours [ i ] == instance )
136
+ if ( childNetworkedBehaviours [ i ] == instance )
130
137
return i ;
131
138
}
132
139
return 0 ;
133
140
}
134
141
135
142
internal NetworkedBehaviour GetBehaviourAtOrderIndex ( ushort index )
136
143
{
137
- NetworkedBehaviour [ ] behaviours = GetComponentsInChildren < NetworkedBehaviour > ( ) ;
138
- return behaviours [ index ] ;
144
+ //TODO index out of bounds
145
+ return childNetworkedBehaviours [ index ] ;
139
146
}
140
147
}
141
148
}
0 commit comments