C# · Programming

Launching a Unity exe in either VR or non-VR mode with a command line argument

The game I’m working on is a racing game that’ll be published on Steam later in the year. I want to support both VR and pancake gamers, so I need to be able to tell which mode to launch the game in and to turn VR on or off at the start of the game.

The first part of the puzzle is to find a good way to tell Unity which mode we want to operate in – to VR, or not to VR. That is the question.

Command line arguments

When we launch a program from either command line or windows shortcuts, we call the executable file with something like this:

RC.exe

If we want to pass in some extra information about how to launch our file, we can add arguments to the end of the filename – they go after the exe and often start with a – sign, something like this:

RC.exe -disableVR

The wrong way

The only problem with trying to detect an argument and disable VR like this, is that VR initialization takes place before your own code even runs. It’s already too late by the time your code reads the argument. Detecting it yourself and shutting down VR will make VR mode start up on first launch, then shut down again. No, we need to pass in a system argument that’ll tell Unity not to crank up the VR in the first place.

The right way

It turns out that we can actually set the VR mode from an argument. For example, if we wanted to start our Unity game in Oculus mode we could tell it to launch in Oculus mode – and the same goes for SteamVR or whatever other ‘modes’ are supported. These modes are the ones you can find under the list of Virtual Reality SDKs in XR Plugin Management (or, on Unity 2019.4 or earlier in Player settings/ XR Settings) and you can find the Unity documentation for all this here.

As well as being able to set the VR mode on launch, we can use -vrmode None to tell Unity to use the ‘None’ mode ie. not launching any of the SDKs. Make sure that your Virtual Reality SDKs list has None in it. If it doesn’t, click on the + icon and add it to the list. Mine looks like this:

So, when I ship my game I’ll have two ways to launch the game from its folder- the regular .EXE file, and a shortcut that includes the -vrmode argument. I name the shortcut ‘No VR’ and now anyone can easily launch whichever version they want. Here’s the properties of my NO VR shortcut:

Perfect! Just launch the game with this and we’re done here, right? Well, not quite. It’s pretty likely that we also need to tell our game not to try and use VR features or to spawn mode-specific elements.

In the case of my game, I have a VR camera rig and a non-VR camera rig. I need to know which mode the game is running in so that I know which one to use.

To find which mode the game is running in, we can call on XRSettings.isDeviceActive like this:

        if (XRSettings.isDeviceActive)
        {
            Debug.Log("RUNNING GAME IN VR MODE!");
        } else
		{
            Debug.Log("RUNNING GAME IN NON-VR MODE!");
        }

Dealing with Multiple Scenes

The way I choose to deal with this (and you may want to do it differently depending on your needs) is to have a ‘vr mode getter’ scene that launches before all of the scenes. My ‘vr mode getter’ scene just has some code to detect the VR mode and set a static variable I can look at everywhere else in the code. Once I’ve detected the mode and set my variable, I load the main menu.

When my main menu scene starts, I check the ‘VREnabled’ static variable to know whether to Instantiate the vr camera rig or the non-vr camera rig. Same when any other scene loads – I just refer to my static variable to see whether vr is enabled or not.

I like to use a config class. It’s a simple class containing static variables you can access from anywhere in the project to know game settings. For example:

using UnityEngine;

public class GameProperties : MonoBehaviour
{
    public static bool VREnabled = false;
    public static int chosenSaveProfile =0;
}

With my config class in place, I can use it when I first check the vr mode. At the very start of the game (in that ‘vr mode getter’ scene I mentioned earlier), I set my GameProperties VREnabled Boolean, like this:

void Start()
    {
        if (XRSettings.isDeviceActive)
        {
            Debug.Log("RUNNING GAME IN VR MODE!");

            GameProperties.VREnabled = true;
        } else
	{
            Debug.Log("RUNNING GAME IN NON-VR MODE!");

            GameProperties.VREnabled = false;
        }

        // LOAD MAIN MENU HERE!
    }

In other Scenes, whenever I need to do something VR mode specific, I just check against GameProperties.VREnabled first. Here’s an example of spawning the camera rigs when my main menu loads:

if (GameProperties.VREnabled)
{
	GameObject camRigGO = Instantiate(_VRRigPrefab);}
else
{
	GameObject camRigGO = Instantiate(_NonVRRigPrefab);
}

Hopefully this gives you an insight into setting up non-vr and vr launch options for your games.


Have fun!