Ejemplo n.º 1
0
 // 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);
         }
     }
 }
Ejemplo n.º 2
0
        internal void FinishLoadScene(string sceneName, SceneOperation sceneOperation)
        {
            // host mode?
            if (client && client.IsLocalClient)
            {
                logger.Log("Finished loading scene in host mode.");

                if (client.Connection != null && sceneOperation == SceneOperation.Normal)
                {
                    client.OnAuthenticated(client.Connection);
                }

                // server scene was loaded. now spawn all the objects
                server.ActivateHostScene();

                // call OnServerSceneChanged
                OnServerSceneChanged(sceneName, sceneOperation);

                if (client.IsConnected)
                {
                    // let client know that we changed scene
                    OnClientSceneChanged(sceneName, sceneOperation);
                }
            }
            // server-only mode?
            else if (server && server.Active)
            {
                logger.Log("Finished loading scene in server-only mode.");

                server.SpawnObjects();
                OnServerSceneChanged(sceneName, sceneOperation);
            }
            // client-only mode?
            else if (client && client.Active)
            {
                logger.Log("Finished loading scene in client-only mode.");

                if (client.Connection != null && sceneOperation == SceneOperation.Normal)
                {
                    client.OnAuthenticated(client.Connection);
                }

                OnClientSceneChanged(sceneName, sceneOperation);
            }
        }
Ejemplo n.º 3
0
        public bool StartServer()
        {
            InitializeSingleton();

            if (runInBackground)
                Application.runInBackground = true;

            ConfigureServerFrameRate();

            if (!NetworkServer.Listen(maxConnections))
            {
                Debug.LogError("StartServer listen failed.");
                return false;
            }

            // call OnStartServer AFTER Listen, so that NetworkServer.active is
            // true and we can call NetworkServer.Spawn in OnStartServer
            // overrides.
            // (useful for loading & spawning stuff from database etc.)
            //
            // note: there is no risk of someone connecting after Listen() and
            //       before OnStartServer() because this all runs in one thread
            //       and we don't start processing connects until Update.
            OnStartServer();

            // this must be after Listen(), since that registers the default message handlers
            RegisterServerMessages();

            if (LogFilter.Debug) Debug.Log("NetworkManager StartServer");
            isNetworkActive = true;

            // Only change scene if the requested online scene is not blank, and is not already loaded
            string loadedSceneName = SceneManager.GetActiveScene().name;
            if (!string.IsNullOrEmpty(onlineScene) && onlineScene != loadedSceneName && onlineScene != offlineScene)
            {
                ServerChangeScene(onlineScene);
            }
            else
            {
                NetworkServer.SpawnObjects();
            }
            return true;
        }
Ejemplo n.º 4
0
        void FinishLoadScene()
        {
            // NOTE: this cannot use NetworkClient.allClients[0] - that client may be for a completely different purpose.

            if (client != null)
            {
                // process queued messages that we received while loading the scene
                if (LogFilter.logDebug)
                {
                    Debug.Log("FinishLoadScene: resuming handlers after scene was loading.");
                }
                NetworkClient.pauseMessageHandling = false;

                if (s_ClientReadyConnection != null)
                {
                    m_ClientLoadedScene = true;
                    OnClientConnect(s_ClientReadyConnection);
                    s_ClientReadyConnection = null;
                }
            }
            else
            {
                if (LogFilter.logDev)
                {
                    Debug.Log("FinishLoadScene client is null");
                }
            }

            if (NetworkServer.active)
            {
                NetworkServer.SpawnObjects();
                OnServerSceneChanged(networkSceneName);
            }

            if (IsClientConnected() && client != null)
            {
                RegisterClientMessages(client);
                OnClientSceneChanged(client.connection);
            }
        }
Ejemplo n.º 5
0
        public bool StartServer()
        {
            InitializeSingleton();

            if (m_RunInBackground)
            {
                Application.runInBackground = true;
            }

            NetworkServer.useWebSockets = m_UseWebSockets;

            if (m_ServerBindToIP && !string.IsNullOrEmpty(m_ServerBindAddress))
            {
                if (!NetworkServer.Listen(m_ServerBindAddress, m_NetworkPort, m_MaxConnections))
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("StartServer listen on " + m_ServerBindAddress + " failed.");
                    }
                    return(false);
                }
            }
            else
            {
                if (!NetworkServer.Listen(m_NetworkPort, m_MaxConnections))
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("StartServer listen failed.");
                    }
                    return(false);
                }
            }

            // call OnStartServer AFTER Listen, so that NetworkServer.active is
            // true and we can call NetworkServer.Spawn in OnStartServer
            // overrides.
            // (useful for loading & spawning stuff from database etc.)
            //
            // note: there is no risk of someone connecting after Listen() and
            //       before OnStartServer() because this all runs in one thread
            //       and we don't start processing connects until Update.
            OnStartServer();

            // this must be after Listen(), since that registers the default message handlers
            RegisterServerMessages();

            if (LogFilter.logDebug)
            {
                Debug.Log("NetworkManager StartServer port:" + m_NetworkPort);
            }
            isNetworkActive = true;

            // Only change scene if the requested online scene is not blank, and is not already loaded
            string loadedSceneName = SceneManager.GetSceneAt(0).name;

            if (!string.IsNullOrEmpty(m_OnlineScene) && m_OnlineScene != loadedSceneName && m_OnlineScene != m_OfflineScene)
            {
                ServerChangeScene(m_OnlineScene);
            }
            else
            {
                NetworkServer.SpawnObjects();
            }
            return(true);
        }