mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
fix: Headless / Dedicated Server detection now works on Unity 2019/2020 without dedicated server build support too (#3643)
This commit is contained in:
parent
3f31cbd99b
commit
42a89a798b
@ -85,9 +85,10 @@ public virtual void Start()
|
|||||||
transport = Transport.active;
|
transport = Transport.active;
|
||||||
|
|
||||||
// Server mode? then start advertising
|
// Server mode? then start advertising
|
||||||
#if UNITY_SERVER
|
if (Utils.IsHeadless())
|
||||||
|
{
|
||||||
AdvertiseServer();
|
AdvertiseServer();
|
||||||
#endif
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long RandomLong()
|
public static long RandomLong()
|
||||||
|
@ -251,10 +251,11 @@ public override void OnServerDisconnect(NetworkConnectionToClient conn)
|
|||||||
OnRoomServerDisconnect(conn);
|
OnRoomServerDisconnect(conn);
|
||||||
base.OnServerDisconnect(conn);
|
base.OnServerDisconnect(conn);
|
||||||
|
|
||||||
#if UNITY_SERVER
|
if (Utils.IsHeadless())
|
||||||
|
{
|
||||||
if (numPlayers < 1)
|
if (numPlayers < 1)
|
||||||
StopServer();
|
StopServer();
|
||||||
#endif
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sequential index used in round-robin deployment of players into instances and score positioning
|
// Sequential index used in round-robin deployment of players into instances and score positioning
|
||||||
|
@ -221,11 +221,10 @@ public virtual void Start()
|
|||||||
//
|
//
|
||||||
// (tick rate is applied in StartServer!)
|
// (tick rate is applied in StartServer!)
|
||||||
//
|
//
|
||||||
#if UNITY_SERVER
|
|
||||||
// don't auto start in editor where we have a UI, only in builds.
|
// don't auto start in editor where we have a UI, only in builds.
|
||||||
// otherwise if we switch to 'Dedicated Server' target and press
|
// otherwise if we switch to 'Dedicated Server' target and press
|
||||||
// Play, it would auto start the server every time.
|
// Play, it would auto start the server every time.
|
||||||
if (!Application.isEditor)
|
if (Utils.IsHeadless() && !Application.isEditor)
|
||||||
{
|
{
|
||||||
if (autoStartServerBuild)
|
if (autoStartServerBuild)
|
||||||
{
|
{
|
||||||
@ -237,7 +236,6 @@ public virtual void Start()
|
|||||||
StartClient();
|
StartClient();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure to call base.Update() when overwriting
|
// make sure to call base.Update() when overwriting
|
||||||
@ -686,10 +684,11 @@ public virtual void OnApplicationQuit()
|
|||||||
// useful for headless benchmark clients.
|
// useful for headless benchmark clients.
|
||||||
public virtual void ConfigureHeadlessFrameRate()
|
public virtual void ConfigureHeadlessFrameRate()
|
||||||
{
|
{
|
||||||
#if UNITY_SERVER
|
if (Utils.IsHeadless())
|
||||||
|
{
|
||||||
Application.targetFrameRate = sendRate;
|
Application.targetFrameRate = sendRate;
|
||||||
// Debug.Log($"Server Tick Rate set to {Application.targetFrameRate} Hz.");
|
// Debug.Log($"Server Tick Rate set to {Application.targetFrameRate} Hz.");
|
||||||
#endif
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool InitializeSingleton()
|
bool InitializeSingleton()
|
||||||
|
@ -120,7 +120,9 @@ public static void Listen(int maxConns)
|
|||||||
|
|
||||||
if (Transport.active is PortTransport portTransport)
|
if (Transport.active is PortTransport portTransport)
|
||||||
{
|
{
|
||||||
#if UNITY_SERVER && !UNITY_EDITOR
|
if (Utils.IsHeadless())
|
||||||
|
{
|
||||||
|
#if !UNITY_EDITOR
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
Console.WriteLine($"Server listening on port {portTransport.Port}");
|
Console.WriteLine($"Server listening on port {portTransport.Port}");
|
||||||
Console.ResetColor();
|
Console.ResetColor();
|
||||||
@ -128,6 +130,7 @@ public static void Listen(int maxConns)
|
|||||||
Debug.Log($"Server listening on port {portTransport.Port}");
|
Debug.Log($"Server listening on port {portTransport.Port}");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
Debug.Log("Server started listening");
|
Debug.Log("Server started listening");
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.Rendering;
|
||||||
using UnityEngine.SceneManagement;
|
using UnityEngine.SceneManagement;
|
||||||
|
|
||||||
namespace Mirror
|
namespace Mirror
|
||||||
@ -32,6 +33,15 @@ public static class Channels
|
|||||||
|
|
||||||
public static class Utils
|
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()
|
public static uint GetTrueRandomUInt()
|
||||||
{
|
{
|
||||||
// use Crypto RNG to avoid having time based duplicates
|
// use Crypto RNG to avoid having time based duplicates
|
||||||
|
@ -76,11 +76,14 @@ Setting showStartButton false when the button is pressed hides it in the game sc
|
|||||||
public override void OnRoomServerPlayersReady()
|
public override void OnRoomServerPlayersReady()
|
||||||
{
|
{
|
||||||
// calling the base method calls ServerChangeScene as soon as all players are in Ready state.
|
// calling the base method calls ServerChangeScene as soon as all players are in Ready state.
|
||||||
#if UNITY_SERVER
|
if (Utils.IsHeadless())
|
||||||
|
{
|
||||||
base.OnRoomServerPlayersReady();
|
base.OnRoomServerPlayersReady();
|
||||||
#else
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
showStartButton = true;
|
showStartButton = true;
|
||||||
#endif
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnGUI()
|
public override void OnGUI()
|
||||||
|
@ -285,13 +285,16 @@ public override void ServerStart()
|
|||||||
|
|
||||||
if (transport is PortTransport portTransport)
|
if (transport is PortTransport portTransport)
|
||||||
{
|
{
|
||||||
#if UNITY_SERVER
|
if (Utils.IsHeadless())
|
||||||
|
{
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
Console.WriteLine($"Server listening on port {portTransport.Port}");
|
Console.WriteLine($"Server listening on port {portTransport.Port}");
|
||||||
Console.ResetColor();
|
Console.ResetColor();
|
||||||
#else
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Debug.Log($"Server listening on port {portTransport.Port}");
|
Debug.Log($"Server listening on port {portTransport.Port}");
|
||||||
#endif
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user