fix: #573 (part 2) NetworkManager detects additive scene loads and respawns objects on server/client again

This commit is contained in:
vis2k 2019-03-24 13:15:39 +01:00
parent c1af84e6bf
commit e521a20052

View File

@ -82,6 +82,9 @@ public virtual void Awake()
networkSceneName = offlineScene; networkSceneName = offlineScene;
InitializeSingleton(); InitializeSingleton();
// setup OnSceneLoaded callback
SceneManager.sceneLoaded += OnSceneLoaded;
} }
// headless mode detection // 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. // 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 // 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. // that we can't use NetworkServer/NetworkClient without a NetworkManager invoking Update anymore.