Notes: Setting up a game with optional VR

The aim is to make the game to be able to run in either VR or non-VR mode, example for this is Elite Dangerous, Subnautica, Star Wars: Squadrons, etc.

First of all we’ll need to setup the project: Setting up a VR game project

Next we’ll need to have the option to enable and disable VR on launch.

  1. Go to Project Settings > XR Plugin Management and disable Initialize XR on startup for PC. This should make the game launch in non-VR mode by default
  2. Enabling and disabling VR in runtime:
    public void StartXR()
    {
        StartCoroutine(StartXRCoroutine());
    }

    IEnumerator StartXRCoroutine()
    {
        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();
        }
    }

    void StopXR()
    {
        Debug.Log("Stopping XR...");
        XRGeneralSettings.Instance.Manager.StopSubsystems();
        XRGeneralSettings.Instance.Manager.DeinitializeLoader();
        Debug.Log("XR stopped completely.");
    }

Alternatively, we can use InitializeLoaderSync() so we don’t have to use Coroutine.

Note that there is some bug with the XR Management, initializing XR for the second time will have some problem in the camera. So it might be best to make it to only enable at start, or force restart if disabling.

Also the even in non-VR mode, we should be using the new input system too.

Reference: https://docs.unity3d.com/Packages/com.unity.xr.management@4.1/manual/EndUser.html

0 thoughts on “Notes: Setting up a game with optional VR”

  1. Pingback: TDTD Devlog#0: Initial Idea – Boon's Devlog and Notes

Leave a Comment

Your email address will not be published. Required fields are marked *