diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index 1722a05ea..b3b644e3e 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -82,6 +82,9 @@ public virtual void Awake() networkSceneName = offlineScene; InitializeSingleton(); + + // setup OnSceneLoaded callback + SceneManager.sceneLoaded += OnSceneLoaded; } // headless mode detection @@ -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.