C# · Programming · VR

Launching a Unity (2019 only) exe in either VR or non-VR mode with a command line argument

Before I get into this, it turns out that Unity have changed how this works in Unity 2020 (possibly by accident, by who knows?) .. so if you’re using Unity 2020 you will need to do things differently. Thankfully, I’ve written up a post about doing that right here. Otherwise, if you’re using Unity 2019 or older, read on!

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 (crash course)

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

Set up your game to default to VR mode

By default, your game should be set up to launch in VR. We will use the arguments through a shortcut to disable VR mode.

Go to the menus Edit>Project Settings, find the Player section in the list and then scroll down to XR Settings and expand them out, if needed:

Make sure that Virtual Reality Supported is checked and that you have the correct SDK(s) in the list below that.

Setting the VR mode from an argument

We can set VR mode from an argument. For example, if we wanted to start our Unity app in SteamVR/OpenVR, we could do that – but that’s not what we want to accomplish here. Instead, we want to use the -vrmode argument to *disable* VR mode. We want users to be able to launch the .exe file to run the app in VR mode, and for them to run it through a shortcut to run it in non-VR mode.

We can tell Unity *not* to use VR by using the mode ‘None’. You do need to make sure that your Virtual Reality SDKs list (in Player settings/XR Settings) has None in it, though. If it doesn’t, you can click on the + icon and add it to the list. Mine SDKs list looks like this (note that it has ‘None’ in it and that we need to get both the spelling and caps exactly right in the argument, for this to work):

Make a shortcut and add the argument to launch it in non-VR mode

Here is the .exe file and shortcut for my game RC Rush:

To make a shortcut from your .exe file, right click on it and choose Create Shortcut. Once you have a shortcut, it’s time to add the argument to it. Right click on the shortcut and choose Properties. Click on the Shortcut tab. Mine looks like this:

In the Target field, we need to add the argument, after the quotes, to the end of the filename. Make sure there is a space between the quotes and the argument.

The argument we need to disable VR mode, then, looks like this:

-vrmode None

Now you can test that out and see if it worked. Your game should launch in VR mode from the .exe and non-VR when you launch from the shortcut.

Detecting the VR mode inside your app

Perfect! Just launch the game with this and we’re done here, right? Well, maybe. 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!");
        }

Cheers!

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