Skip to content
This repository has been archived by the owner on Nov 8, 2019. It is now read-only.

Switching between VR and non VR at runtime

Fred Sauer edited this page Nov 15, 2017 · 14 revisions

To be able to switch between VR and non-VR at runtime in your app:

  1. Enable VR support to make it possible for your app to use VR mode:

    Player Settings > Android/iOS > Virtual Reality Supported

  2. In the list of VR SDKs (Player Settings > Android/iOS > Virtual Reality SDKs):

    • Add Daydream and/or Cardboard as appropriate.
    • Add None. This allows yours app to use non-VR mode.
    • Reorder the SDKs:
      • If you want your app to start in VR mode, make sure Daydream or Cardboard is listed first.
      • If you want your app to start in non-VR mode, make sure None is listed first.
  3. To determine which VR SDKs are available at runtime on the current platform, use VRSettings.supportedDevices.

    Note:

    • Returned names are lowercase: "daydream", "cardboard"

    • The None device is returned as the empty string: ""

    • Returned names are those that are available from list you configured:

      Player Settings > Android/iOS > Virtual Reality SDKs

  4. To switch from non-VR to VR mode at runtime, make sure you wait at least one frame between calling VRSettings.LoadDeviceByName() and setting VRSettings.enabled to true.

    In this example call StartCoroutine(SwitchToVR()) from your code:

    IEnumerator SwitchToVR() {
      VRSettings.LoadDeviceByName("daydream"); // Or "cardboard" (both lowercase).

      // Wait one frame!
      yield return null;

      // Now it's ok to enable VR mode.
      VRSettings.enabled = true;
    }
  1. To switch from VR mode to non-VR mode at runtime either set VRSettings.enabled to false (and leave the current VR SDK loaded so you can later just toggle it back to true), or load the None ("") non-VR device.

    In this example, call StartCoroutine(SwitchOutOfVr()) from your code:

    IEnumerator SwitchOutOfVr() {
      VRSettings.LoadDeviceByName(""); // Empty string loads the "None" device.

      // Wait one frame!
      yield return null;

      // Not needed, loading the None (`""`) device automatically sets `VRSettings.enabled` to `false`.
      // VRSettings.enabled = false;

      // If you only have one camera in your scene, you can just call `Camera.main.ResetAspect()` instead.
      ResetCameras();
    }
  1. Add a ResetCameras() method, used by SwitchOutOfVr():
    // Resets local rotation and calls `ResetAspect()` on all enabled VR cameras.
    void ResetCameras() {
      // Camera looping logic copied from GvrEditorEmulator.cs
      for (int i = 0; i < Camera.allCameras.Length; i++) {
        Camera cam = Camera.allCameras[i];
        if (cam.enabled && cam.stereoTargetEye != StereoTargetEyeMask.None) {
          // Reset local rotation. (Only required if you change the local rotation while in non-VR mode.)
          cam.transform.localRotation = Quaternion.identity;
          // Reset local position. (Only required if you change the local position while in non-VR mode.)
          cam.transform.localPosition = Vector3.zero;
          // Reset aspect ratio based on normal (non-VR) screen size.
          // Required in certain versions of Unity, see github.com/googlevr/gvr-unity-sdk/issues/628
          cam.ResetAspect();
          // Don't need to reset camera `fieldOfView`, since it's restored to the original value automatically.
        }
      }
    }
  1. (Optional) For easy testing, automatically toggle between VR and non-VR (2D) modes:
  IEnumerator Start() {
    // Every 10 seconds toggle VR mode
    while (true) {
      yield return new WaitForSeconds(10f);
      yield return SwitchToVR();

      yield return new WaitForSeconds(10f);
      yield return SwitchOutOfVr();
    }
  }
Clone this wiki locally