-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Switching between VR and non VR at runtime
To be able to switch between VR and non-VR at runtime in your app:
-
Enable VR support to make it possible for your app to use VR mode:
Player Settings > Android/iOS > Virtual Reality Supported
-
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.
-
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
-
-
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
totrue
.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;
}
-
To switch from VR mode to non-VR mode at runtime either set
VRSettings.enabled
tofalse
(and leave the current VR SDK loaded so you can later just toggle it back totrue
), 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();
}
- Add a
ResetCameras()
method, used bySwitchOutOfVr()
:
// 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.
}
}
}
- (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();
}
}