0
\$\begingroup\$

Lots of games have options for launching in VR or Desktop modes, either through steam launch options ("Play game", or "Play game in VR") or command line flags (-vrmode none, --no-vr) at least from a user perspective. Phasmophobia, which was developed with Unity (engine version unknown) uses the -vrmode none option to force the desktop version of the game. I'm guessing this was in the 2019 version of unity, as that still supported the dev SKD options under Project Settings -> Player -> XR. However with the newer 2020 LTS version of unity, using the OpenXR Package, I don't see an equivalent.

I'd like to give a command line option to my game to launch in either VR mode or Desktop mode, then stay in that mode for the game session. Is there a way to do this with the OpenXR package?

Engine: Unity 2020.3.15f2
Template: Universal Render Pipeline
XR Interaction Toolkit Package: 1.0.0-pre.5 [Preview]
XR Plugin Management: 4.0.7

Only OpenXR is selected in XR Plug-In Management. This is a bare-bones project with an XR rig and a single intractable for testing.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

After more work and communicating with people, I managed to figure this out myself.

For other people who find this post, here is the solution for VR and non-VR in a single build.

Note that this will start your game in 2D, NOT VR. After it loads past the unity splash screen, it will switch to VR.

You need to create an empty game object in your scene (you do not have to switch scenes for this to work) and attach the below script to it. After this, build your game and launch the game with the "--enable-vr" flag after the game.exe. In a Windows Shortcut, this looks like "C:\Path\To\Game.exe" --enable-vr

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Management;
 
public class VRSwitcher : MonoBehaviour
{
    public void Awake()
    {
        StartCoroutine(StartXRCoroutine());
    }
 
    // This function checks out startup arguments to see if we want VR
    // To do this, create a desktop shortcut and add the arg at the end.
    // Example: "C:\Path\To\Game.exe" --enable-vr
    private static bool GetArg(string name)
    {
        var args = System.Environment.GetCommandLineArgs();
        for (int i = 0; i < args.Length; i++)
        {
            Debug.Log($"Arg {i}: {args[i]}");
            if (args[i] == name)
            {
                return true;
            }
        }
        return false;
    }
    // From unity docs
    // https://docs.unity3d.com/Packages/[email protected]/manual/EndUser.html
    public IEnumerator StartXRCoroutine()
    {
        var enableVRArg = "--enable-vr";
 
        // Only run the code block when we want VR
        Debug.Log("Looking if VR should enable");
        if (GetArg(enableVRArg))
        {
            Debug.Log("Initializing XR...");
            yield return XRGeneralSettings.Instance.Manager.InitializeLoader();
 
            if (XRGeneralSettings.Instance.Manager.activeLoader == null)
            {
                Debug.LogError("Initializing XR Failed. Check Editor or Player log for details.");
            }
            else
            {
                Debug.Log("Starting XR...");
                XRGeneralSettings.Instance.Manager.StartSubsystems();
            }
        }
        else
        {
            Debug.Log("Did not find VR arg, starting in 2D");
        }
    }
}

I hope this helps someone

\$\endgroup\$
2
  • \$\begingroup\$ How funny is it that I am updating Phasmophobia to OpenXR and Unity 2020 and I am looking to do exactly this! Will these command line args work when launched via steam as well? Thanks for sharing your script! :) \$\endgroup\$
    – Lavender
    Commented Mar 23, 2022 at 10:46
  • \$\begingroup\$ I gotta say, that's pretty crazy! From what I know about how your game was launched and the way Steam works, I don't see any reason why it shouldn't work. I have not gotten far enough to test with Steam however, so I guess you'll just have to give it a try! If you could let us know back here if it did work in Steam, that would be a great contribution. Additionally, thanks for inspiring me to figure this out! I had no idea it was possible in a single build until I played your game \$\endgroup\$ Commented Mar 24, 2022 at 13:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .