diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 7a6f07054..d50958f67 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -104,7 +104,6 @@ internal static void RegisterSystemHandlers(bool hostMode) if (hostMode) { RegisterHandler(msg => {}, false); - RegisterHandler(OnHostClientSpawn); // host mode doesn't need spawning RegisterHandler(msg => {}); // host mode doesn't need spawning @@ -115,11 +114,11 @@ internal static void RegisterSystemHandlers(bool hostMode) else { RegisterHandler(NetworkTime.OnClientPong, false); - RegisterHandler(OnSpawn); RegisterHandler(OnObjectSpawnStarted); RegisterHandler(OnObjectSpawnFinished); RegisterHandler(OnEntityStateMessage); } + RegisterHandler(OnSpawn); RegisterHandler(OnObjectDestroy); RegisterHandler(OnObjectHide); RegisterHandler(OnRPCMessage); @@ -1107,24 +1106,7 @@ static void ClearNullFromSpawned() removeFromSpawned.Clear(); } - // host mode callbacks ///////////////////////////////////////////////// - internal static void OnHostClientSpawn(SpawnMessage message) - { - if (NetworkIdentity.spawned.TryGetValue(message.netId, out NetworkIdentity localObject) && - localObject != null) - { - if (message.isLocalPlayer) - InternalAddPlayer(localObject); - - localObject.hasAuthority = message.isOwner; - localObject.NotifyAuthority(); - localObject.OnStartClient(); - localObject.OnSetHostVisibility(true); - CheckForLocalPlayer(localObject); - } - } - - // client-only mode callbacks ////////////////////////////////////////// + // callbacks /////////////////////////////////////////////////////////// static void OnEntityStateMessage(EntityStateMessage message) { // Debug.Log("NetworkClient.OnUpdateVarsMessage " + msg.netId); @@ -1188,10 +1170,30 @@ internal static void OnObjectDestroy(ObjectDestroyMessage message) internal static void OnSpawn(SpawnMessage message) { - // Debug.Log($"Client spawn handler instantiating netId={msg.netId} assetID={msg.assetId} sceneId={msg.sceneId:X} pos={msg.position}"); - if (FindOrSpawnObject(message, out NetworkIdentity identity)) + // host mode + if (isHostClient) { - ApplySpawnPayload(identity, message); + if (NetworkIdentity.spawned.TryGetValue(message.netId, out NetworkIdentity localObject) && + localObject != null) + { + if (message.isLocalPlayer) + InternalAddPlayer(localObject); + + localObject.hasAuthority = message.isOwner; + localObject.NotifyAuthority(); + localObject.OnStartClient(); + localObject.OnSetHostVisibility(true); + CheckForLocalPlayer(localObject); + } + } + // client only + else + { + // Debug.Log($"Client spawn handler instantiating netId={msg.netId} assetID={msg.assetId} sceneId={msg.sceneId:X} pos={msg.position}"); + if (FindOrSpawnObject(message, out NetworkIdentity identity)) + { + ApplySpawnPayload(identity, message); + } } } diff --git a/Assets/Mirror/Tests/Runtime/ClientSceneTests_LocalPlayer_AsHost.cs b/Assets/Mirror/Tests/Runtime/ClientSceneTests_LocalPlayer_AsHost.cs index cc0c83afc..7402aa0c2 100644 --- a/Assets/Mirror/Tests/Runtime/ClientSceneTests_LocalPlayer_AsHost.cs +++ b/Assets/Mirror/Tests/Runtime/ClientSceneTests_LocalPlayer_AsHost.cs @@ -22,7 +22,7 @@ public IEnumerator LocalPlayerIsSetToNullAfterNetworkDestroy() NetworkIdentity.spawned[msg.netId] = identity; - NetworkClient.OnHostClientSpawn(msg); + NetworkClient.OnSpawn(msg); NetworkServer.Destroy(identity.gameObject); @@ -47,7 +47,7 @@ public IEnumerator LocalPlayerIsSetToNullAfterNetworkUnspawn() }; NetworkIdentity.spawned[msg.netId] = identity; - NetworkClient.OnHostClientSpawn(msg); + NetworkClient.OnSpawn(msg); NetworkServer.UnSpawn(identity.gameObject);