Distributing a game with optional VR mode.

Is it possible to distribute a game that is non-VR with the ability to turn on VR at runtime if a headset is present? Or do you need to make separate apps, one VR and one regular?

It is possible, though not based on detecting a head set. You have to manually enable/disable VR and provide the users a chance to toggle that.

Without knowing what version of Unity you are using, and what type of VR system (Built in or XR Plugin) I can’t really tell you more. However, there are plentiful resources for both in regards to how to manually enable/disable VR/XR.

Thanks for the response.

I’m using Unity 2018.4.22. I’m using built-in Oculus support.

Do I need to distribute my app with “Virtual Reality Supported” turned on (in Player Settings)? Or should I leave it off and turn it on at runtime?

Leave it on, add the None device as the first device and load the Oculus device when you want to switch to VR. Then load the None device to disable VR when you want to switch out.

1 Like

Sounds good, thanks.

1 Like

I (and a few others) have posted here asking specifically for docs or info on exactly how to do this with XR Plugin system and had no reply.

I’m sure it’s somwhere, but it’s missing from the official docs, and I haven’t been able to find anything for the new system specifically.

If you could give me a link (or a short description, if that’s easier) that would be awesome (and I’ll add it to the FAQ, hopefully others won’t need to keep asking the same question in future :)).

2 Likes

Has there been any more info on this? I’m attempting to do the exact same thing (optional vr launch mode at game start) and having a hard time figuring it out

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

Here’s docs on how to manage XR manually: stEnd-user documentation | XR Plugin Management | 4.0.7 (unity3d.com)

@joejo I’ve read through that today and implemented it on an empty game object, then called that using StartCoroutine(StartXRCoroutine()) in the awake function of the script. However this did not work - SteamVR did not open, and if I already had it open, my HMD was not moving the camera. Is this the correct way to do this? My controls work if I check the “Start XR on Initialization” in the XR plugin manager, so I know it’s a problem with the initialization code

Without seeing your project I don’t know what the issue is at hand. There is this forum post: Resolved - [XR Management] Controlling the used XRLoader Manually - Unity Forum

That may help. As well there may be more info on the Valve forums for direct support of steam vr.

If none of that helps, and you have a small, simple repro project I may be able to look at it and see.

@joejo In cleaning up a little of my code to upload to github, I found a mistake: I had put the function in the Start() method this time around instead of Awake(). I actually have this working now. Please, pin this somewhere as I’ve seen about 50 dead-end threads on this today, and can’t believe I finally got it working! I believe this can also finally be marked as resolved!

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/com.unity.xr.management@4.0/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");
        }
    }
}
4 Likes

Good idea, done.

1 Like

This code does not work for me, I am using a windows mixed reality headset, and unity version 2021.3.28f. While it does track the headset movement, the camera is completely dark.