Skip to content

Commit

Permalink
fix: #573 (part 2) NetworkManager detects additive scene loads and re…
Browse files Browse the repository at this point in the history
…spawns objects on server/client again
  • Loading branch information
miwarnec committed Mar 24, 2019
1 parent c1af84e commit e521a20
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Assets/Mirror/Runtime/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public virtual void Awake()
networkSceneName = offlineScene;

InitializeSingleton();

// setup OnSceneLoaded callback
SceneManager.sceneLoaded += OnSceneLoaded;
}

// headless mode detection
Expand Down Expand Up @@ -146,6 +149,33 @@ public virtual void Start()
}
}

// support additive scene loads:
// NetworkScenePostProcess disables all scene objects on load, and
// * NetworkServer.SpawnObjects enables them again on the server when
// calling OnStartServer
// * ClientScene.PrepareToSpawnSceneObjects enables them again on the
// client after the server sends ObjectSpawnStartedMessage to client
// in SpawnObserversForConnection. this is only called when the
// client joins, so we need to rebuild scene objects manually again
// TODO merge this with FinishLoadScene()?
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
if (mode == LoadSceneMode.Additive)
{
if (NetworkServer.active)
{
// TODO only respawn the server objects from that scene later!
NetworkServer.SpawnObjects();
Debug.Log("Respawned Server objects after additive scene load: " + scene.name);
}
if (NetworkClient.active)
{
ClientScene.PrepareToSpawnSceneObjects();
Debug.Log("Rebuild Client spawnableObjects after additive scene load: " + scene.name);
}
}
}

// NetworkIdentity.UNetStaticUpdate is called from UnityEngine while LLAPI network is active.
// if we want TCP then we need to call it manually. probably best from NetworkManager, although this means
// that we can't use NetworkServer/NetworkClient without a NetworkManager invoking Update anymore.
Expand Down

0 comments on commit e521a20

Please sign in to comment.