diff --git a/Assets/Mirror/Components/Discovery/NetworkDiscoveryBase.cs b/Assets/Mirror/Components/Discovery/NetworkDiscoveryBase.cs index 8e13f01d0..441734402 100644 --- a/Assets/Mirror/Components/Discovery/NetworkDiscoveryBase.cs +++ b/Assets/Mirror/Components/Discovery/NetworkDiscoveryBase.cs @@ -85,9 +85,10 @@ public virtual void Start() transport = Transport.active; // Server mode? then start advertising -#if UNITY_SERVER - AdvertiseServer(); -#endif + if (Utils.IsHeadless()) + { + AdvertiseServer(); + } } public static long RandomLong() diff --git a/Assets/Mirror/Components/NetworkRoomManager.cs b/Assets/Mirror/Components/NetworkRoomManager.cs index e210e730f..11f771431 100644 --- a/Assets/Mirror/Components/NetworkRoomManager.cs +++ b/Assets/Mirror/Components/NetworkRoomManager.cs @@ -251,10 +251,11 @@ public override void OnServerDisconnect(NetworkConnectionToClient conn) OnRoomServerDisconnect(conn); base.OnServerDisconnect(conn); -#if UNITY_SERVER - if (numPlayers < 1) - StopServer(); -#endif + if (Utils.IsHeadless()) + { + if (numPlayers < 1) + StopServer(); + } } // Sequential index used in round-robin deployment of players into instances and score positioning diff --git a/Assets/Mirror/Core/NetworkManager.cs b/Assets/Mirror/Core/NetworkManager.cs index 05c1813fe..22ae8cff1 100644 --- a/Assets/Mirror/Core/NetworkManager.cs +++ b/Assets/Mirror/Core/NetworkManager.cs @@ -221,11 +221,10 @@ public virtual void Start() // // (tick rate is applied in StartServer!) // -#if UNITY_SERVER // don't auto start in editor where we have a UI, only in builds. // otherwise if we switch to 'Dedicated Server' target and press // Play, it would auto start the server every time. - if (!Application.isEditor) + if (Utils.IsHeadless() && !Application.isEditor) { if (autoStartServerBuild) { @@ -237,7 +236,6 @@ public virtual void Start() StartClient(); } } -#endif } // make sure to call base.Update() when overwriting @@ -686,10 +684,11 @@ public virtual void OnApplicationQuit() // useful for headless benchmark clients. public virtual void ConfigureHeadlessFrameRate() { -#if UNITY_SERVER - Application.targetFrameRate = sendRate; - // Debug.Log($"Server Tick Rate set to {Application.targetFrameRate} Hz."); -#endif + if (Utils.IsHeadless()) + { + Application.targetFrameRate = sendRate; + // Debug.Log($"Server Tick Rate set to {Application.targetFrameRate} Hz."); + } } bool InitializeSingleton() diff --git a/Assets/Mirror/Core/NetworkServer.cs b/Assets/Mirror/Core/NetworkServer.cs index 9b57b28c1..067cde9d1 100644 --- a/Assets/Mirror/Core/NetworkServer.cs +++ b/Assets/Mirror/Core/NetworkServer.cs @@ -120,13 +120,16 @@ public static void Listen(int maxConns) if (Transport.active is PortTransport portTransport) { -#if UNITY_SERVER && !UNITY_EDITOR - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine($"Server listening on port {portTransport.Port}"); - Console.ResetColor(); + if (Utils.IsHeadless()) + { +#if !UNITY_EDITOR + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine($"Server listening on port {portTransport.Port}"); + Console.ResetColor(); #else - Debug.Log($"Server listening on port {portTransport.Port}"); + Debug.Log($"Server listening on port {portTransport.Port}"); #endif + } } else Debug.Log("Server started listening"); diff --git a/Assets/Mirror/Core/Tools/Utils.cs b/Assets/Mirror/Core/Tools/Utils.cs index 2ed8dffd0..0c44eb014 100644 --- a/Assets/Mirror/Core/Tools/Utils.cs +++ b/Assets/Mirror/Core/Tools/Utils.cs @@ -2,6 +2,7 @@ using System.Runtime.CompilerServices; using System.Security.Cryptography; using UnityEngine; +using UnityEngine.Rendering; using UnityEngine.SceneManagement; namespace Mirror @@ -32,6 +33,15 @@ public static class Channels public static class Utils { + // detect headless / dedicated server mode + // in Unity 2019/2020, we need to check if there is no graphics device. + // in Unity 2021+, we could check for the #if UNITY_SERVER define. + // checking graphics device is safest. + // => it works on all Unity versions. + // => it keeps all the code active instead of ifdefing it out. + public static bool IsHeadless() => + SystemInfo.graphicsDeviceType == GraphicsDeviceType.Null; + public static uint GetTrueRandomUInt() { // use Crypto RNG to avoid having time based duplicates diff --git a/Assets/Mirror/Examples/Room/Scripts/NetworkRoomManagerExt.cs b/Assets/Mirror/Examples/Room/Scripts/NetworkRoomManagerExt.cs index 29f1ccbf6..069913dfb 100644 --- a/Assets/Mirror/Examples/Room/Scripts/NetworkRoomManagerExt.cs +++ b/Assets/Mirror/Examples/Room/Scripts/NetworkRoomManagerExt.cs @@ -76,11 +76,14 @@ Setting showStartButton false when the button is pressed hides it in the game sc public override void OnRoomServerPlayersReady() { // calling the base method calls ServerChangeScene as soon as all players are in Ready state. -#if UNITY_SERVER - base.OnRoomServerPlayersReady(); -#else - showStartButton = true; -#endif + if (Utils.IsHeadless()) + { + base.OnRoomServerPlayersReady(); + } + else + { + showStartButton = true; + } } public override void OnGUI() diff --git a/Assets/Mirror/Transports/Multiplex/MultiplexTransport.cs b/Assets/Mirror/Transports/Multiplex/MultiplexTransport.cs index e85ee809c..fbd2abf61 100644 --- a/Assets/Mirror/Transports/Multiplex/MultiplexTransport.cs +++ b/Assets/Mirror/Transports/Multiplex/MultiplexTransport.cs @@ -285,13 +285,16 @@ public override void ServerStart() if (transport is PortTransport portTransport) { -#if UNITY_SERVER - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine($"Server listening on port {portTransport.Port}"); - Console.ResetColor(); -#else - Debug.Log($"Server listening on port {portTransport.Port}"); -#endif + if (Utils.IsHeadless()) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine($"Server listening on port {portTransport.Port}"); + Console.ResetColor(); + } + else + { + Debug.Log($"Server listening on port {portTransport.Port}"); + } } } }