/// <summary> /// Stops the current mode of the NetworkManager /// </summary> private static void StopNetworkManagerMode() { switch (CurrentNetworkManagerMode) { case NetworkManagerOperatingMode.Host: { // Stop the host NetworkManagerObject.StopHost(); break; } case NetworkManagerOperatingMode.Server: { // Stop the server NetworkManagerObject.StopServer(); break; } case NetworkManagerOperatingMode.Client: { // Stop the client NetworkManagerObject.StopClient(); break; } } Debug.Log($"{CurrentNetworkManagerMode} stopped."); CurrentNetworkManagerMode = NetworkManagerOperatingMode.None; }
/// <summary> /// Called upon the RpcQueueTests being instantiated. /// This creates an instance of the NetworkManager to be used during unit tests. /// Currently, the best method to run unit tests is by starting in host mode as you can /// send messages to yourself (i.e. Host-Client to Host-Server and vice versa). /// As such, the default setting is to start in Host mode. /// </summary> /// <param name="managerMode">parameter to specify which mode you want to start the NetworkManager</param> /// <param name="networkConfig">parameter to specify custom NetworkConfig settings</param> /// <returns>true if it was instantiated or is already instantiate otherwise false means it failed to instantiate</returns> public static bool StartNetworkManager(out NetworkManager networkManager, NetworkManagerOperatingMode managerMode = NetworkManagerOperatingMode.Host, NetworkConfig networkConfig = null) { // If we are changing the current manager mode and the current manager mode is not "None", then stop the NetworkManager mode if (CurrentNetworkManagerMode != managerMode && CurrentNetworkManagerMode != NetworkManagerOperatingMode.None) { StopNetworkManagerMode(); } if (NetworkManagerGameObject == null) { NetworkManagerGameObject = new GameObject(nameof(NetworkManager)); NetworkManagerObject = NetworkManagerGameObject.AddComponent <NetworkManager>(); if (NetworkManagerObject == null) { networkManager = null; return(false); } Debug.Log($"{nameof(NetworkManager)} Instantiated."); var unetTransport = NetworkManagerGameObject.AddComponent <UNetTransport>(); if (networkConfig == null) { networkConfig = new NetworkConfig { EnableSceneManagement = false, RegisteredScenes = new List <string>() { SceneManager.GetActiveScene().name } }; } else { networkConfig.RegisteredScenes.Add(SceneManager.GetActiveScene().name); } NetworkManagerObject.NetworkConfig = networkConfig; unetTransport.ConnectAddress = "127.0.0.1"; unetTransport.ConnectPort = 7777; unetTransport.ServerListenPort = 7777; unetTransport.MessageBufferSize = 65535; unetTransport.MaxConnections = 100; unetTransport.MessageSendMode = UNetTransport.SendMode.Immediately; NetworkManagerObject.NetworkConfig.NetworkTransport = unetTransport; // Starts the network manager in the mode specified StartNetworkManagerMode(managerMode); } networkManager = NetworkManagerObject; return(true); }
/// <summary> /// Starts the NetworkManager in the current mode specified by managerMode /// </summary> /// <param name="managerMode">the mode to start the NetworkManager as</param> private static void StartNetworkManagerMode(NetworkManagerOperatingMode managerMode) { CurrentNetworkManagerMode = managerMode; switch (CurrentNetworkManagerMode) { case NetworkManagerOperatingMode.Host: { // Starts the host NetworkManagerObject.StartHost(); break; } case NetworkManagerOperatingMode.Server: { // Starts the server NetworkManagerObject.StartServer(); break; } case NetworkManagerOperatingMode.Client: { // Starts the client NetworkManagerObject.StartClient(); break; } } // If we started an MLAPI session if (CurrentNetworkManagerMode != NetworkManagerOperatingMode.None) { // With some unit tests the Singleton can still be from a previous unit test // depending upon the order of operations that occurred. if (NetworkManager.Singleton != NetworkManagerObject) { NetworkManagerObject.SetSingleton(); } // Only log this if we started an MLAPI session Debug.Log($"{CurrentNetworkManagerMode} started."); } }