@@ -210,13 +210,61 @@ public void NetworkShow(ulong clientId, Stream payload = null)
210210 {
211211 throw new NotServerException ( "Only server can change visibility" ) ;
212212 }
213+
214+ if ( observers . Contains ( clientId ) )
215+ {
216+ throw new VisibilityChangeException ( "The object is already visible" ) ;
217+ }
213218
214- if ( ! observers . Contains ( clientId ) )
219+ // Send spawn call
220+ observers . Add ( clientId ) ;
221+
222+ SpawnManager . SendSpawnCallForObject ( clientId , this , payload ) ;
223+ }
224+
225+ /// <summary>
226+ /// Shows a list of previously hidden objects to a client
227+ /// </summary>
228+ /// <param name="networkedObjects">The objects to show</param>
229+ /// <param name="clientId">The client to show the objects to</param>
230+ /// <param name="payload">An optional payload to send as part of the spawns</param>
231+ public static void NetworkShow ( List < NetworkedObject > networkedObjects , ulong clientId , Stream payload = null )
232+ {
233+ if ( ! NetworkingManager . Singleton . IsServer )
234+ {
235+ throw new NotServerException ( "Only server can change visibility" ) ;
236+ }
237+
238+ // Do the safety loop first to prevent putting the MLAPI in an invalid state.
239+ for ( int i = 0 ; i < networkedObjects . Count ; i ++ )
240+ {
241+ if ( ! networkedObjects [ i ] . IsSpawned )
242+ {
243+ throw new SpawnStateException ( "Object is not spawned" ) ;
244+ }
245+
246+ if ( networkedObjects [ i ] . observers . Contains ( clientId ) )
247+ {
248+ throw new VisibilityChangeException ( "NetworkedObject with NetworkId: " + networkedObjects [ i ] . NetworkId + " is already visible" ) ;
249+ }
250+ }
251+
252+ using ( PooledBitStream stream = PooledBitStream . Get ( ) )
215253 {
216- // Send spawn call
217- observers . Add ( clientId ) ;
254+ using ( PooledBitWriter writer = PooledBitWriter . Get ( stream ) )
255+ {
256+ writer . WriteUInt16Packed ( ( ushort ) networkedObjects . Count ) ;
257+ }
218258
219- SpawnManager . SendSpawnCallForObject ( clientId , this , payload ) ;
259+ for ( int i = 0 ; i < networkedObjects . Count ; i ++ )
260+ {
261+ // Send spawn call
262+ networkedObjects [ i ] . observers . Add ( clientId ) ;
263+
264+ SpawnManager . WriteSpawnCallForObject ( stream , clientId , networkedObjects [ i ] , payload ) ;
265+ }
266+
267+ InternalMessageSender . Send ( MLAPIConstants . MLAPI_ADD_OBJECTS , "MLAPI_INTERNAL" , stream , SecuritySendFlags . None , null ) ;
220268 }
221269 }
222270
@@ -235,21 +283,80 @@ public void NetworkHide(ulong clientId)
235283 {
236284 throw new NotServerException ( "Only server can change visibility" ) ;
237285 }
238-
239- if ( observers . Contains ( clientId ) && clientId != NetworkingManager . Singleton . ServerClientId )
286+
287+ if ( ! observers . Contains ( clientId ) )
288+ {
289+ throw new VisibilityChangeException ( "The object is already hidden" ) ;
290+ }
291+
292+ if ( clientId == NetworkingManager . Singleton . ServerClientId )
240293 {
241- // Send destroy call
242- observers . Remove ( clientId ) ;
294+ throw new VisibilityChangeException ( "Cannot hide an object from the server" ) ;
295+ }
296+
297+
298+ // Send destroy call
299+ observers . Remove ( clientId ) ;
243300
244- using ( PooledBitStream stream = PooledBitStream . Get ( ) )
301+ using ( PooledBitStream stream = PooledBitStream . Get ( ) )
302+ {
303+ using ( PooledBitWriter writer = PooledBitWriter . Get ( stream ) )
245304 {
246- using ( PooledBitWriter writer = PooledBitWriter . Get ( stream ) )
247- {
248- writer . WriteUInt64Packed ( NetworkId ) ;
305+ writer . WriteUInt64Packed ( NetworkId ) ;
249306
250- InternalMessageSender . Send ( MLAPIConstants . MLAPI_DESTROY_OBJECT , "MLAPI_INTERNAL" , stream , SecuritySendFlags . None , null ) ;
307+ InternalMessageSender . Send ( MLAPIConstants . MLAPI_DESTROY_OBJECT , "MLAPI_INTERNAL" , stream , SecuritySendFlags . None , null ) ;
308+ }
309+ }
310+ }
311+
312+ /// <summary>
313+ /// Hides a list of objects from a client
314+ /// </summary>
315+ /// <param name="networkedObjects">The objects to hide</param>
316+ /// <param name="clientId">The client to hide the objects from</param>
317+ public static void NetworkHide ( List < NetworkedObject > networkedObjects , ulong clientId )
318+ {
319+ if ( ! NetworkingManager . Singleton . IsServer )
320+ {
321+ throw new NotServerException ( "Only server can change visibility" ) ;
322+ }
323+
324+ if ( clientId == NetworkingManager . Singleton . ServerClientId )
325+ {
326+ throw new VisibilityChangeException ( "Cannot hide an object from the server" ) ;
327+ }
328+
329+ // Do the safety loop first to prevent putting the MLAPI in an invalid state.
330+ for ( int i = 0 ; i < networkedObjects . Count ; i ++ )
331+ {
332+ if ( ! networkedObjects [ i ] . IsSpawned )
333+ {
334+ throw new SpawnStateException ( "Object is not spawned" ) ;
335+ }
336+
337+ if ( ! networkedObjects [ i ] . observers . Contains ( clientId ) )
338+ {
339+ throw new VisibilityChangeException ( "NetworkedObject with NetworkId: " + networkedObjects [ i ] . NetworkId + " is already hidden" ) ;
340+ }
341+ }
342+
343+
344+ using ( PooledBitStream stream = PooledBitStream . Get ( ) )
345+ {
346+ using ( PooledBitWriter writer = PooledBitWriter . Get ( stream ) )
347+ {
348+ writer . WriteUInt16Packed ( ( ushort ) networkedObjects . Count ) ;
349+
350+ for ( int i = 0 ; i < networkedObjects . Count ; i ++ )
351+ {
352+ // Send destroy call
353+ networkedObjects [ i ] . observers . Remove ( clientId ) ;
354+
355+ writer . WriteUInt64Packed ( networkedObjects [ i ] . NetworkId ) ;
251356 }
252357 }
358+
359+ InternalMessageSender . Send ( MLAPIConstants . MLAPI_DESTROY_OBJECTS , "MLAPI_INTERNAL" , stream , SecuritySendFlags . None , null ) ;
253360 }
254361 }
255362
0 commit comments