fbpx

How to start Unity application from command prompt with initial arguments?

Why use the command line?

As we all know, there are times when we have to use a command line for Unity. For instance, when you don’t want to launch the app in a regular way, such as through clicking the .exe file.

Let’s get practical and see some real examples. Imagine two cases, a simpler one that takes a singular true/false bool argument and one more complicated.

First case:

We have a project, and we want to pass it on to external testing. The testers told us that they need the app in two modes – one for a regular player and a developer mode with cheats and settings that we’d usually use in the editor during development (screenshots, free camera).

In our app, we can add a simple function that takes a bool parameter called “runDeveloperMode”. If we launch the application in a default way, the flag is set to false, which could be set to true from the command line. This way we have a convenient way to switch the app into developer mode, without preparing multiple builds.

SEE ALSO:  Build levels smoothly with Snaps in Unity

Second case:

In this scenario, we make a multiplayer server. To set it up we have multiple variables, like:

  • game type (Deathmatch)
  • maximum player number
  • modes (friendly fire, gravity mode, pickup mode and other elements we want)
  • enemy strength
  • server type (local/remote)
  • game time limit
  • etc.

As you can see, there are too many variables to validate preparing a different build every time, therefore we need to have some sort of gameplay manager, that would send the necessary information about the room settings after entering the game lobby.

After receiving the data, the server launches the necessary variables from the command line. This is a more advanced usage of the command line because in this case, we’re not able to set it otherwise.

A practical use case:

Let’s imagine a simple app that will allow the user to change its resolution and window mode through the command line. For this example to work, we prepared a CommandLineController script that we attach to any object on the scene.

using System;
using UnityEngine;

public class CommandLineController : MonoBehaviour
{
     #region MEMBERS

     private const string windowModeArg = "isWindowedMode=";
     private const string resolutionWidthArg = "width=";
     private const string resolutionHeightArg = "height=";

     [SerializeField]
     private int defaultWidth = 1920;
     [SerializeField]
     private int defaultHeight = 1080;

     #endregion

     #region PROPERTIES

     public int DefaultHeight {
        get { return defaultHeight; }
     }

     public int DefaultWidth {
        get { return defaultWidth; }
     }

     #endregion

     #region FUNCTIONS

     void Start()
     {
        ParseCommandLineArguments();
     }

     private void ParseCommandLineArguments()
     {
        string[] args = System.Environment.GetCommandLineArgs();

        int screenWidth = DefaultWidth;
        int screenHeight = DefaultHeight;
        bool isWindowsMode = false;
        string argumentString;
        for (int i = 0; i < args.Length; i++)
        {
          argumentString = "";
          if (args[i].StartsWith(windowModeArg) == true)
          {
              argumentString = args[i].Replace(windowModeArg, "");
              Boolean.TryParse(argumentString, out isWindowsMode)
          }
          else if (args[i].StartsWith(resolutionWidthArg) == true)
          {
              argumentString = args[i].Replace(resolutionWidthArg, "");
              Int32.TryParse(argumentString, out screenWidth);
          }
          else if (args[i].StartsWith(resolutionHeightArg) == true)
          {
              argumentString = args[i].Replace(resolutionHeightArg, "");
              Int32.TryParse(argumentString, out screenHeight);
          }
     }

     Screen.SetResolution(screenWidth, screenHeight, isWindowsMode == false);
     }

     #endregion

     #region CLASS_ENUMS

     #endregion
}

Upon start, the script checks for any command-line arguments. If it finds any arguments fitting the width, height or is windowed mode parameters, it tries to parse them and then change the screen resolution and FullScreen mode accordingly.

SEE ALSO:  Build levels smoothly with Snaps in Unity

Because Unity stores the last set resolution for the player in the registry, we declared two fields that store the default width and height for the script to use.

So, if our default resolution is 1920×1080 and the app is in FullScreen mode, launching it with the following parameters:

C:Users<user name>DesktopCommandLineCommandLineExample.exe width=640 height=480 isWindowedMode=true

will cause the app to change the resolution to 640×480 and display in a window.

As we can see, command line parameters can be used to customize any aspect of the app upon launch. By removing the need for preparing multiple different builds for different parties like testers or clients, this very handy feature saves us time and makes it easier to manage build configurations.

SEE ALSO:  Build levels smoothly with Snaps in Unity

From resolution changes and developer-mode options to more robust server configurations – accessing the parameters is very simple and can be easily integrated into an existing app.

Share with:


4Experience Crew - 10/25/2019 - Tutorials - 0 Comments

Leave a comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.


PL OFFICE:
ul. Widok 5
43-300 Bielsko-Biała, PL
NIP/VAT ID:PL547-215-50-20
REGON:362196623
KRS: 0000574950

US OFFICE:
350 S. Northwest Highway SUITE 300 Park Ridge, Illinois, 60068, USA

Logo_4experience_white-scroll

Copyright 4experience ©  All Rights Reserved | Privacy & Policy