fix: Headless / Dedicated Server detection now works on Unity 2019/2020 without dedicated server build support too (#3643)

This commit is contained in:
mischa 2023-11-07 18:06:29 +01:00 committed by GitHub
parent 3f31cbd99b
commit 42a89a798b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 51 additions and 31 deletions

View File

@ -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()

View File

@ -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

View File

@ -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()

View File

@ -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");

View File

@ -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

View File

@ -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()

View File

@ -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}");
}
}
}
}