-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
NetworkClient.cs
1111 lines (957 loc) · 46.7 KB
/
NetworkClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
using Guid = System.Guid;
using Object = UnityEngine.Object;
namespace Mirror
{
public enum ConnectState
{
None,
Connecting,
Connected,
Disconnected
}
/// <summary>
/// This is a network client class used by the networking system. It contains a NetworkConnection that is used to connect to a network server.
/// <para>The <see cref="NetworkClient">NetworkClient</see> handle connection state, messages handlers, and connection configuration. There can be many <see cref="NetworkClient">NetworkClient</see> instances in a process at a time, but only one that is connected to a game server (<see cref="NetworkServer">NetworkServer</see>) that uses spawned objects.</para>
/// <para><see cref="NetworkClient">NetworkClient</see> has an internal update function where it handles events from the transport layer. This includes asynchronous connect events, disconnect events and incoming data from a server.</para>
/// <para>The <see cref="NetworkManager">NetworkManager</see> has a NetworkClient instance that it uses for games that it starts, but the NetworkClient may be used by itself.</para>
/// </summary>
[DisallowMultipleComponent]
public class NetworkClient : MonoBehaviour
{
[Header("Authentication")]
[Tooltip("Authentication component attached to this object")]
public NetworkAuthenticator authenticator;
/// <summary>
/// The registered network message handlers.
/// </summary>
readonly Dictionary<int, NetworkMessageDelegate> handlers = new Dictionary<int, NetworkMessageDelegate>();
// spawn handlers
readonly Dictionary<Guid, SpawnHandlerDelegate> spawnHandlers = new Dictionary<Guid, SpawnHandlerDelegate>();
readonly Dictionary<Guid, UnSpawnDelegate> unspawnHandlers = new Dictionary<Guid, UnSpawnDelegate>();
[Serializable] public class NetworkConnectionEvent : UnityEvent<NetworkConnectionToServer> { }
public NetworkConnectionEvent Connected = new NetworkConnectionEvent();
public NetworkConnectionEvent Authenticated = new NetworkConnectionEvent();
/// <summary>
/// The NetworkConnection object this client is using.
/// </summary>
public NetworkConnection connection { get; internal set; }
/// <summary>
/// NetworkIdentity of the localPlayer
/// </summary>
public NetworkIdentity localPlayer { get; private set; }
internal ConnectState connectState = ConnectState.None;
/// <summary>
/// active is true while a client is connecting/connected
/// (= while the network is active)
/// </summary>
public bool active => connectState == ConnectState.Connecting || connectState == ConnectState.Connected;
/// <summary>
/// This gives the current connection status of the client.
/// </summary>
public bool isConnected => connectState == ConnectState.Connected;
/// <summary>
/// List of prefabs that will be registered with the spawning system.
/// <para>For each of these prefabs, ClientManager.RegisterPrefab() will be automatically invoke.</para>
/// </summary>
public List<GameObject> spawnPrefabs = new List<GameObject>();
readonly Dictionary<uint, NetworkIdentity> spawned = new Dictionary<uint, NetworkIdentity>();
public readonly NetworkTime Time = new NetworkTime();
bool isSpawnFinished;
/// <summary>
/// Returns true when a client's connection has been set to ready.
/// <para>A client that is ready recieves state updates from the server, while a client that is not ready does not. This useful when the state of the game is not normal, such as a scene change or end-of-game.</para>
/// <para>This is read-only. To change the ready state of a client, use ClientScene.Ready(). The server is able to set the ready state of clients using NetworkServer.SetClientReady(), NetworkServer.SetClientNotReady() and NetworkServer.SetAllClientsNotReady().</para>
/// <para>This is done when changing scenes so that clients don't receive state update messages during scene loading.</para>
/// </summary>
public bool ready { get; internal set; }
/// <summary>
/// This is a dictionary of the prefabs that are registered on the client with ClientScene.RegisterPrefab().
/// <para>The key to the dictionary is the prefab asset Id.</para>
/// </summary>
private readonly Dictionary<Guid, GameObject> prefabs = new Dictionary<Guid, GameObject>();
/// <summary>
/// This is dictionary of the disabled NetworkIdentity objects in the scene that could be spawned by messages from the server.
/// <para>The key to the dictionary is the NetworkIdentity sceneId.</para>
/// </summary>
public Dictionary<ulong, NetworkIdentity> spawnableObjects;
/// <summary>
/// List of all objects spawned in this client
/// </summary>
public Dictionary<uint, NetworkIdentity> Spawned
{
get
{
// if we are in host mode, the list of spawned object is the same as the server list
if (hostServer != null)
return hostServer.spawned;
else
return spawned;
}
}
/// <summary>
/// The host server
/// </summary>
NetworkServer hostServer;
/// <summary>
/// NetworkClient can connect to local server in host mode too
/// </summary>
public bool isLocalClient => hostServer != null;
void Start()
{
InitializeAuthEvents();
}
/// <summary>
/// Connect client to a NetworkServer instance.
/// </summary>
/// <param name="address"></param>
public async Task ConnectAsync(string serverIp)
{
if (LogFilter.Debug) Debug.Log("Client Connect: " + serverIp);
RegisterSystemHandlers(false);
Transport.activeTransport.enabled = true;
InitializeTransportHandlers();
RegisterSpawnPrefabs();
connectState = ConnectState.Connecting;
await Transport.activeTransport.ClientConnectAsync(serverIp);
// setup all the handlers
connection = new NetworkConnectionToServer();
connection.SetHandlers(handlers);
OnConnected();
}
/// <summary>
/// Connect client to a NetworkServer instance.
/// </summary>
/// <param name="uri">Address of the server to connect to</param>
public async Task ConnectAsync(Uri uri)
{
if (LogFilter.Debug) Debug.Log("Client Connect: " + uri);
RegisterSystemHandlers(false);
Transport.activeTransport.enabled = true;
InitializeTransportHandlers();
RegisterSpawnPrefabs();
connectState = ConnectState.Connecting;
await Transport.activeTransport.ClientConnectAsync(uri);
// setup all the handlers
connection = new NetworkConnectionToServer();
connection.SetHandlers(handlers);
OnConnected();
}
internal void ConnectHost(NetworkServer server)
{
if (LogFilter.Debug) Debug.Log("Client Connect Host to Server");
RegisterSystemHandlers(true);
InitializeAuthEvents();
connectState = ConnectState.Connected;
// create local connection objects and connect them
(ULocalConnectionToServer connectionToServer, ULocalConnectionToClient connectionToClient)
= ULocalConnectionToClient.CreateLocalConnections();
connection = connectionToServer;
connection.SetHandlers(handlers);
// create server connection to local client
server.SetLocalConnection(this, connectionToClient);
Connected.Invoke(connectionToServer);
hostServer = server;
}
/// <summary>
/// connect host mode
/// </summary>
internal void ConnectLocalServer(NetworkServer server)
{
server.OnConnected(server.localConnection);
}
void InitializeTransportHandlers()
{
Transport.activeTransport.OnClientDataReceived.AddListener(OnDataReceived);
Transport.activeTransport.OnClientDisconnected.AddListener(OnDisconnected);
Transport.activeTransport.OnClientError.AddListener(OnError);
}
void InitializeAuthEvents()
{
if (authenticator != null)
{
authenticator.OnStartClient();
authenticator.OnClientAuthenticated += OnAuthenticated;
Connected.AddListener(authenticator.OnClientAuthenticateInternal);
}
else
{
// if no authenticator, consider connection as authenticated
Connected.AddListener(OnAuthenticated);
}
}
void OnError(Exception exception)
{
Debug.LogException(exception);
}
void OnDisconnected()
{
connectState = ConnectState.Disconnected;
connection?.InvokeHandler(new DisconnectMessage(), -1);
HandleClientDisconnect(connection);
}
/// <summary>
/// client that received the message
/// </summary>
/// <remarks>This is a hack, but it is needed to deserialize
/// gameobjects when processing the message</remarks>
///
internal static NetworkClient Current { get; set; }
internal void OnDataReceived(ArraySegment<byte> data, int channelId)
{
if (connection != null)
{
connection.TransportReceive(data, channelId);
}
else throw new InvalidOperationException("Skipped Data message handling because connection is null.");
}
void OnConnected()
{
// reset network time stats
Time.Reset();
// the handler may want to send messages to the client
// thus we should set the connected state before calling the handler
connectState = ConnectState.Connected;
Time.UpdateClient(this);
Connected.Invoke((NetworkConnectionToServer)connection);
}
void OnAuthenticated(NetworkConnectionToServer conn)
{
// set connection to authenticated
conn.isAuthenticated = true;
Authenticated?.Invoke(conn);
}
/// <summary>
/// Disconnect from server.
/// <para>The disconnect message will be invoked.</para>
/// </summary>
public void Disconnect()
{
connectState = ConnectState.Disconnected;
// local or remote connection?
if (isLocalClient)
{
if (isConnected)
{
hostServer.localConnection.Send(new DisconnectMessage());
}
hostServer.RemoveLocalConnection();
}
else
{
if (connection != null)
{
connection.Disconnect();
RemoveTransportHandlers();
}
}
HandleClientDisconnect(connection);
}
void RemoveTransportHandlers()
{
// so that we don't register them more than once
Transport.activeTransport.OnClientDataReceived.RemoveListener(OnDataReceived);
Transport.activeTransport.OnClientDisconnected.RemoveListener(OnDisconnected);
Transport.activeTransport.OnClientError.RemoveListener(OnError);
}
/// <summary>
/// This sends a network message with a message Id to the server. This message is sent on channel zero, which by default is the reliable channel.
/// <para>The message must be an instance of a class derived from MessageBase.</para>
/// <para>The message id passed to Send() is used to identify the handler function to invoke on the server when the message is received.</para>
/// </summary>
/// <typeparam name="T">The message type to unregister.</typeparam>
/// <param name="message"></param>
/// <param name="channelId"></param>
/// <returns>True if message was sent.</returns>
public bool Send<T>(T message, int channelId = Channels.DefaultReliable) where T : IMessageBase
{
if (connection != null)
{
if (connectState != ConnectState.Connected)
{
Debug.LogError("NetworkClient Send when not connected to a server");
return false;
}
return connection.Send(message, channelId);
}
Debug.LogError("NetworkClient Send with no connection");
return false;
}
internal void Update()
{
// local connection?
if (connection is ULocalConnectionToServer localConnection)
{
localConnection.Update();
}
// remote connection?
else
{
// only update things while connected
if (active && connectState == ConnectState.Connected)
{
Time.UpdateClient(this);
}
}
}
internal void RegisterSystemHandlers(bool hostMode)
{
// host mode client / regular client react to some messages differently.
// but we still need to add handlers for all of them to avoid
// 'message id not found' errors.
if (hostMode)
{
RegisterHandler<ObjectDestroyMessage>(OnHostClientObjectDestroy);
RegisterHandler<ObjectHideMessage>(OnHostClientObjectHide);
RegisterHandler<NetworkPongMessage>((conn, msg) => { }, false);
RegisterHandler<SpawnMessage>(OnHostClientSpawn);
// host mode doesn't need spawning
RegisterHandler<ObjectSpawnStartedMessage>((conn, msg) => { });
// host mode doesn't need spawning
RegisterHandler<ObjectSpawnFinishedMessage>((conn, msg) => { });
RegisterHandler<UpdateVarsMessage>((conn, msg) => { });
}
else
{
RegisterHandler<ObjectDestroyMessage>(OnObjectDestroy);
RegisterHandler<ObjectHideMessage>(OnObjectHide);
RegisterHandler<NetworkPongMessage>(Time.OnClientPong, false);
RegisterHandler<SpawnMessage>(OnSpawn);
RegisterHandler<ObjectSpawnStartedMessage>(OnObjectSpawnStarted);
RegisterHandler<ObjectSpawnFinishedMessage>(OnObjectSpawnFinished);
RegisterHandler<UpdateVarsMessage>(OnUpdateVarsMessage);
}
RegisterHandler<RpcMessage>(OnRPCMessage);
RegisterHandler<SyncEventMessage>(OnSyncEventMessage);
}
/// <summary>
/// Register a handler for a particular message type.
/// <para>There are several system message types which you can add handlers for. You can also add your own message types.</para>
/// </summary>
/// <typeparam name="T">The message type to unregister.</typeparam>
/// <param name="handler"></param>
/// <param name="requireAuthentication">true if the message requires an authenticated connection</param>
public void RegisterHandler<T>(Action<NetworkConnectionToServer, T> handler, bool requireAuthentication = true) where T : IMessageBase, new()
{
int msgType = MessagePacker.GetId<T>();
if (handlers.ContainsKey(msgType))
{
if (LogFilter.Debug) Debug.Log("NetworkClient.RegisterHandler replacing " + handler + " - " + msgType);
}
handlers[msgType] = NetworkConnection.MessageHandler(handler, requireAuthentication);
}
/// <summary>
/// Register a handler for a particular message type.
/// <para>There are several system message types which you can add handlers for. You can also add your own message types.</para>
/// </summary>
/// <typeparam name="T">The message type to unregister.</typeparam>
/// <param name="handler"></param>
/// <param name="requireAuthentication">true if the message requires an authenticated connection</param>
public void RegisterHandler<T>(Action<T> handler, bool requireAuthentication = true) where T : IMessageBase, new()
{
RegisterHandler((NetworkConnectionToServer _, T value) => { handler(value); }, requireAuthentication);
}
/// <summary>
/// Unregisters a network message handler.
/// </summary>
/// <typeparam name="T">The message type to unregister.</typeparam>
public void UnregisterHandler<T>() where T : IMessageBase
{
// use int to minimize collisions
int msgType = MessagePacker.GetId<T>();
handlers.Remove(msgType);
}
/// <summary>
/// Shut down a client.
/// <para>This should be done when a client is no longer going to be used.</para>
/// </summary>
public void Shutdown()
{
if (LogFilter.Debug) Debug.Log("Shutting down client.");
ClearSpawners();
DestroyAllClientObjects();
spawnableObjects = null;
connection = null;
ready = false;
isSpawnFinished = false;
connectState = ConnectState.None;
handlers.Clear();
if (authenticator != null)
authenticator.OnClientAuthenticated -= OnAuthenticated;
// disconnect the client connection.
// we do NOT call Transport.Shutdown, because someone only called
// NetworkClient.Shutdown. we can't assume that the server is
// supposed to be shut down too!
Transport.activeTransport.ClientDisconnect();
}
static bool ConsiderForSpawning(NetworkIdentity identity)
{
// not spawned yet, not hidden, etc.?
return !identity.gameObject.activeSelf &&
identity.gameObject.hideFlags != HideFlags.NotEditable &&
identity.gameObject.hideFlags != HideFlags.HideAndDontSave &&
identity.sceneId != 0;
}
/// <summary>
/// This adds a player GameObject for this client.
/// <para>This causes an AddPlayer message to be sent to the server, and NetworkManager.OnServerAddPlayer is called.</para>
/// </summary>
/// <returns>True if player was added.</returns>
public bool AddPlayer() => AddPlayer(null);
/// <summary>
/// This adds a player GameObject for this client. This causes an AddPlayer message to be sent to the server, and NetworkManager.OnServerAddPlayer is called. If an extra message was passed to AddPlayer, then OnServerAddPlayer will be called with a NetworkReader that contains the contents of the message.
/// <para>extraMessage can contain character selection, etc.</para>
/// </summary>
/// <param name="readyConn">The connection to become ready for this client.</param>
/// <param name="extraData">An extra message object that can be passed to the server for this player.</param>
/// <returns>True if player was added.</returns>
public bool AddPlayer(NetworkConnection readyConn)
{
// ensure valid ready connection
if (readyConn != null)
{
ready = true;
connection = readyConn;
}
if (!ready)
{
Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
return false;
}
if (connection.identity != null)
{
Debug.LogError("ClientScene.AddPlayer: a PlayerController was already added. Did you call AddPlayer twice?");
return false;
}
if (LogFilter.Debug) Debug.Log("ClientScene.AddPlayer() called with connection [" + connection + "]");
var message = new AddPlayerMessage();
connection.Send(message);
return true;
}
/// <summary>
/// Removes the player from the game.
/// </summary>
/// <returns>True if succcessful</returns>
public bool RemovePlayer()
{
if (LogFilter.Debug) Debug.Log("ClientScene.RemovePlayer() called with connection [" + connection + "]");
if (connection.identity != null)
{
connection.Send(new RemovePlayerMessage());
Destroy(connection.identity.gameObject);
connection.identity = null;
localPlayer = null;
return true;
}
return false;
}
/// <summary>
/// Signal that the client connection is ready to enter the game.
/// <para>This could be for example when a client enters an ongoing game and has finished loading the current scene. The server should respond to the SYSTEM_READY event with an appropriate handler which instantiates the players object for example.</para>
/// </summary>
/// <param name="conn">The client connection which is ready.</param>
/// <returns>True if succcessful</returns>
public bool Ready(NetworkConnection conn)
{
if (ready)
{
Debug.LogError("A connection has already been set as ready. There can only be one.");
return false;
}
if (LogFilter.Debug) Debug.Log("ClientScene.Ready() called with connection [" + conn + "]");
if (conn != null)
{
// Set these before sending the ReadyMessage, otherwise host client
// will fail in InternalAddPlayer with null readyConnection.
ready = true;
connection = conn;
connection.isReady = true;
// Tell server we're ready to have a player object spawned
conn.Send(new ReadyMessage());
return true;
}
Debug.LogError("Ready() called with invalid connection object: conn=null");
return false;
}
// this is called from message handler for Owner message
internal void InternalAddPlayer(NetworkIdentity identity)
{
if (LogFilter.Debug) Debug.LogWarning("ClientScene.InternalAddPlayer");
// NOTE: It can be "normal" when changing scenes for the player to be destroyed and recreated.
// But, the player structures are not cleaned up, we'll just replace the old player
localPlayer = identity;
if (connection != null)
{
connection.identity = identity;
}
else
{
Debug.LogWarning("No ready connection found for setting player controller during InternalAddPlayer");
}
}
internal void HandleClientDisconnect(NetworkConnection conn)
{
if (connection == conn && ready)
{
ready = false;
connection = null;
}
}
/// <summary>
/// Call this after loading/unloading a scene in the client after connection to register the spawnable objects
/// </summary>
public void PrepareToSpawnSceneObjects()
{
// add all unspawned NetworkIdentities to spawnable objects
spawnableObjects = Resources.FindObjectsOfTypeAll<NetworkIdentity>()
.Where(ConsiderForSpawning)
.ToDictionary(identity => identity.sceneId, identity => identity);
}
NetworkIdentity SpawnSceneObject(ulong sceneId)
{
if (spawnableObjects.TryGetValue(sceneId, out NetworkIdentity identity))
{
spawnableObjects.Remove(sceneId);
return identity;
}
Debug.LogWarning("Could not find scene object with sceneid:" + sceneId.ToString("X"));
return null;
}
#region Spawn Prefabs
private void RegisterSpawnPrefabs()
{
for (int i = 0; i < spawnPrefabs.Count; i++)
{
GameObject prefab = spawnPrefabs[i];
if (prefab != null)
{
RegisterPrefab(prefab);
}
}
}
/// <summary>
/// Find the registered prefab for this asset id.
/// Useful for debuggers
/// </summary>
/// <param name="assetId">asset id of the prefab</param>
/// <param name="prefab">the prefab gameobject</param>
/// <returns>true if prefab was registered</returns>
public bool GetPrefab(Guid assetId, out GameObject prefab)
{
prefab = null;
return assetId != Guid.Empty &&
prefabs.TryGetValue(assetId, out prefab) && prefab != null;
}
/// <summary>
/// Registers a prefab with the spawning system.
/// <para>When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instantiate a corresponding client object with the same netId.</para>
/// <para>The NetworkManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene.</para>
/// <para>The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of NetworkAssetIds and prefab references.</para>
/// </summary>
/// <param name="prefab">A Prefab that will be spawned.</param>
/// <param name="newAssetId">An assetId to be assigned to this prefab. This allows a dynamically created game object to be registered for an already known asset Id.</param>
public void RegisterPrefab(GameObject prefab, Guid newAssetId)
{
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity)
{
identity.assetId = newAssetId;
if (LogFilter.Debug) Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + identity.assetId);
prefabs[identity.assetId] = prefab;
}
else
{
Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
}
}
/// <summary>
/// Registers a prefab with the spawning system.
/// <para>When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instantiate a corresponding client object with the same netId.</para>
/// <para>The NetworkManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene.</para>
/// <para>The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of NetworkAssetIds and prefab references.</para>
/// </summary>
/// <param name="prefab">A Prefab that will be spawned.</param>
public void RegisterPrefab(GameObject prefab)
{
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity)
{
if (LogFilter.Debug) Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + identity.assetId);
prefabs[identity.assetId] = prefab;
NetworkIdentity[] identities = prefab.GetComponentsInChildren<NetworkIdentity>();
if (identities.Length > 1)
{
Debug.LogWarning("The prefab '" + prefab.name +
"' has multiple NetworkIdentity components. There can only be one NetworkIdentity on a prefab, and it must be on the root object.");
}
}
else
{
Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
}
}
/// <summary>
/// Registers a prefab with the spawning system.
/// <para>When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instantiate a corresponding client object with the same netId.</para>
/// <para>The NetworkManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene.</para>
/// <para>The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of NetworkAssetIds and prefab references.</para>
/// </summary>
/// <param name="prefab">A Prefab that will be spawned.</param>
/// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
/// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
public void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
{
RegisterPrefab(prefab, msg => spawnHandler(msg.position, msg.assetId), unspawnHandler);
}
/// <summary>
/// Registers a prefab with the spawning system.
/// <para>When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instantiate a corresponding client object with the same netId.</para>
/// <para>The NetworkManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene.</para>
/// <para>The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of NetworkAssetIds and prefab references.</para>
/// </summary>
/// <param name="prefab">A Prefab that will be spawned.</param>
/// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
/// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
public void RegisterPrefab(GameObject prefab, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
{
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity == null)
{
Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
return;
}
if (spawnHandler == null || unspawnHandler == null)
{
Debug.LogError("RegisterPrefab custom spawn function null for " + identity.assetId);
return;
}
if (identity.assetId == Guid.Empty)
{
Debug.LogError("RegisterPrefab game object " + prefab.name + " has no prefab. Use RegisterSpawnHandler() instead?");
return;
}
if (LogFilter.Debug) Debug.Log("Registering custom prefab '" + prefab.name + "' as asset:" + identity.assetId + " " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName());
spawnHandlers[identity.assetId] = spawnHandler;
unspawnHandlers[identity.assetId] = unspawnHandler;
}
/// <summary>
/// Removes a registered spawn prefab that was setup with ClientScene.RegisterPrefab.
/// </summary>
/// <param name="prefab">The prefab to be removed from registration.</param>
public void UnregisterPrefab(GameObject prefab)
{
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity == null)
{
Debug.LogError("Could not unregister '" + prefab.name + "' since it contains no NetworkIdentity component");
return;
}
spawnHandlers.Remove(identity.assetId);
unspawnHandlers.Remove(identity.assetId);
}
#endregion
#region Spawn Handler
/// <summary>
/// This is an advanced spawning function that registers a custom assetId with the UNET spawning system.
/// <para>This can be used to register custom spawning methods for an assetId - instead of the usual method of registering spawning methods for a prefab. This should be used when no prefab exists for the spawned objects - such as when they are constructed dynamically at runtime from configuration data.</para>
/// </summary>
/// <param name="assetId">Custom assetId string.</param>
/// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
/// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
public void RegisterSpawnHandler(Guid assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
{
RegisterSpawnHandler(assetId, msg => spawnHandler(msg.position, msg.assetId), unspawnHandler);
}
/// <summary>
/// This is an advanced spawning function that registers a custom assetId with the UNET spawning system.
/// <para>This can be used to register custom spawning methods for an assetId - instead of the usual method of registering spawning methods for a prefab. This should be used when no prefab exists for the spawned objects - such as when they are constructed dynamically at runtime from configuration data.</para>
/// </summary>
/// <param name="assetId">Custom assetId string.</param>
/// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
/// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
public void RegisterSpawnHandler(Guid assetId, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
{
if (spawnHandler == null || unspawnHandler == null)
{
Debug.LogError("RegisterSpawnHandler custom spawn function null for " + assetId);
return;
}
if (LogFilter.Debug) Debug.Log("RegisterSpawnHandler asset '" + assetId + "' " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName());
spawnHandlers[assetId] = spawnHandler;
unspawnHandlers[assetId] = unspawnHandler;
}
/// <summary>
/// Removes a registered spawn handler function that was registered with ClientScene.RegisterHandler().
/// </summary>
/// <param name="assetId">The assetId for the handler to be removed for.</param>
public void UnregisterSpawnHandler(Guid assetId)
{
spawnHandlers.Remove(assetId);
unspawnHandlers.Remove(assetId);
}
/// <summary>
/// This clears the registered spawn prefabs and spawn handler functions for this client.
/// </summary>
public void ClearSpawners()
{
prefabs.Clear();
spawnHandlers.Clear();
unspawnHandlers.Clear();
}
#endregion
void UnSpawn(NetworkIdentity identity)
{
Guid assetId = identity.assetId;
identity.NetworkDestroy();
if (unspawnHandlers.TryGetValue(assetId, out UnSpawnDelegate handler) && handler != null)
{
handler(identity.gameObject);
}
else if (identity.sceneId == 0)
{
Destroy(identity.gameObject);
}
else
{
identity.MarkForReset();
identity.gameObject.SetActive(false);
spawnableObjects[identity.sceneId] = identity;
}
}
/// <summary>
/// Destroys all networked objects on the client.
/// <para>This can be used to clean up when a network connection is closed.</para>
/// </summary>
public void DestroyAllClientObjects()
{
foreach (NetworkIdentity identity in Spawned.Values)
{
if (identity != null && identity.gameObject != null)
{
UnSpawn(identity);
}
}
Spawned.Clear();
}
void ApplySpawnPayload(NetworkIdentity identity, SpawnMessage msg)
{
identity.Reset();
if (msg.assetId != Guid.Empty)
identity.assetId = msg.assetId;
if (!identity.gameObject.activeSelf)
{
identity.gameObject.SetActive(true);
}
// apply local values for VR support
identity.transform.localPosition = msg.position;
identity.transform.localRotation = msg.rotation;
identity.transform.localScale = msg.scale;
identity.hasAuthority = msg.isOwner;
identity.netId = msg.netId;
identity.server = hostServer;
identity.client = this;
if (msg.isLocalPlayer)
InternalAddPlayer(identity);
// deserialize components if any payload
// (Count is 0 if there were no components)
if (msg.payload.Count > 0)
{
using (PooledNetworkReader payloadReader = NetworkReaderPool.GetReader(msg.payload))
{
identity.OnDeserializeAllSafely(payloadReader, true);
}
}
Spawned[msg.netId] = identity;
// objects spawned as part of initial state are started on a second pass
if (isSpawnFinished)
{
identity.NotifyAuthority();
identity.StartClient();
CheckForLocalPlayer(identity);
}
}
internal void OnSpawn(SpawnMessage msg)
{
if (msg.assetId == Guid.Empty && msg.sceneId == 0)
{
Debug.LogError("OnObjSpawn netId: " + msg.netId + " has invalid asset Id");
return;
}
if (LogFilter.Debug) Debug.Log($"Client spawn handler instantiating netId={msg.netId} assetID={msg.assetId} sceneId={msg.sceneId} pos={msg.position}");
// was the object already spawned?
NetworkIdentity identity = GetExistingObject(msg.netId);
if (identity == null)
{
identity = msg.sceneId == 0 ? SpawnPrefab(msg) : SpawnSceneObject(msg);
}
if (identity == null)
{
Debug.LogError($"Could not spawn assetId={msg.assetId} scene={msg.sceneId} netId={msg.netId}");
return;
}
ApplySpawnPayload(identity, msg);
}
NetworkIdentity GetExistingObject(uint netid)
{
Spawned.TryGetValue(netid, out NetworkIdentity localObject);
return localObject;
}
NetworkIdentity SpawnPrefab(SpawnMessage msg)
{
if (GetPrefab(msg.assetId, out GameObject prefab))
{
GameObject obj = Object.Instantiate(prefab, msg.position, msg.rotation);
if (LogFilter.Debug)
{
Debug.Log("Client spawn handler instantiating [netId:" + msg.netId + " asset ID:" + msg.assetId + " pos:" + msg.position + " rotation: " + msg.rotation + "]");
}
return obj.GetComponent<NetworkIdentity>();
}
if (spawnHandlers.TryGetValue(msg.assetId, out SpawnHandlerDelegate handler))
{
GameObject obj = handler(msg);
if (obj == null)
{
Debug.LogWarning("Client spawn handler for " + msg.assetId + " returned null");
return null;
}
return obj.GetComponent<NetworkIdentity>();
}
Debug.LogError("Failed to spawn server object, did you forget to add it to the NetworkManager? assetId=" + msg.assetId + " netId=" + msg.netId);
return null;
}
NetworkIdentity SpawnSceneObject(SpawnMessage msg)
{
NetworkIdentity spawnedId = SpawnSceneObject(msg.sceneId);
if (spawnedId == null)
{
Debug.LogError("Spawn scene object not found for " + msg.sceneId.ToString("X") + " SpawnableObjects.Count=" + spawnableObjects.Count);
// dump the whole spawnable objects dict for easier debugging
if (LogFilter.Debug)
{
foreach (KeyValuePair<ulong, NetworkIdentity> kvp in spawnableObjects)
Debug.Log("Spawnable: SceneId=" + kvp.Key + " name=" + kvp.Value.name);
}
}
if (LogFilter.Debug) Debug.Log("Client spawn for [netId:" + msg.netId + "] [sceneId:" + msg.sceneId + "] obj:" + spawnedId);
return spawnedId;
}
internal void OnObjectSpawnStarted(ObjectSpawnStartedMessage _)
{
if (LogFilter.Debug) Debug.Log("SpawnStarted");
PrepareToSpawnSceneObjects();
isSpawnFinished = false;
}
internal void OnObjectSpawnFinished(ObjectSpawnFinishedMessage _)
{
if (LogFilter.Debug) Debug.Log("SpawnFinished");
// paul: Initialize the objects in the same order as they were initialized
// in the server. This is important if spawned objects
// use data from scene objects
foreach (NetworkIdentity identity in Spawned.Values.OrderBy(uv => uv.netId))
{
identity.NotifyAuthority();
identity.StartClient();
CheckForLocalPlayer(identity);
}
isSpawnFinished = true;
}