Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Determine if Unity is running in batch mode/headless mode

Discussion in 'Scripting' started by ryanzec, Apr 13, 2011.

  1. ryanzec

    ryanzec

    Joined:
    Jun 10, 2008
    Posts:
    696
    Is there a way through code to determine if the game is running in batch mode/headless mode?
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    ask for the graphics device. In batchmode Unity will initialize a null device
     
  3. ryanzec

    ryanzec

    Joined:
    Jun 10, 2008
    Posts:
    696
    I tried to add this script to an empty game object:

    Code (csharp):
    1. class BatchModeStarter : MonoBehaviour
    2. {
    3.     public void Awake()
    4.     {
    5.         if (SystemInfo.graphicsDeviceName == null)
    6.         {
    7.             Network.InitializeServer(32, 25001, false);
    8.         }
    9.         else
    10.         {
    11.             Debug.Log(SystemInfo.graphicsDeviceName);
    12.         }
    13.     }
    14. }
    and when I try to run this in batch mode, the server does not seem to be started (can't connect to it and know sure what other way to test this then trying to connect). Is this what you were talking about? Any way to test batchmode in the player?
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    What I meant with null device is that the device name will be "Null ????" something, not that it won't have a name at all as it still has a device, but one that just does "nothing"
     
  5. ryanzec

    ryanzec

    Joined:
    Jun 10, 2008
    Posts:
    696
    Checking SystemInfo.graphicsDeviceID == 0 works.
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    thanks for sharing ryanzec :)
     
  7. gresolio

    gresolio

    Joined:
    Jan 5, 2014
    Posts:
    17
    Detect Headless Mode in Unity
    Code (CSharp):
    1.     public static bool IsHeadlessMode()
    2.     {
    3.         return UnityEngine.SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Null;
    4.     }
     
    SavaCoder, zeiksz and TaleOf4Gamers like this.
  8. Unity_Javier

    Unity_Javier

    Unity Technologies

    Joined:
    Mar 7, 2018
    Posts:
    188