This site has been permanently archived. The content on this site was last updated in 2019.
Building hybrid apps in Unity
Stay organized with collections
Save and categorize content based on your preferences.
Hybrid apps support both 2D (non-VR) and VR modes.
Developers can choose whether a hybrid app starts in 2D or in VR mode.
However, Daydream apps must be configured to start in VR mode when launched
from Daydream home.
Developers are responsible for providing users a way to switch
between modes.
While in 2D, apps can optionally allow users to preview content inside a
Magic Window view, which may
or may not be full screen.
Prerequisites
To enable switching between 2D and VR at runtime in your app:
Ensure Player Settings > Android/iOS > Virtual Reality Supported is
checked.
Add the following entries in Player Settings > Android/iOS > Virtual
Reality SDKs:
Order the SDK options to control which mode your app starts in when
launched from the 2D desktop launcher:
To start in 2D mode, put None first.
To start in VR mode, put Daydream or Cardboard first.
Switching to VR mode at runtime
To enter VR mode at runtime, load the desired VR device, wait one frame, then
enable VR mode. Here is an example:
// Call via `StartCoroutine(SwitchToVR())` from your code. Or, use
// `yield SwitchToVR()` if calling from inside another coroutine.
IEnumerator SwitchToVR() {
// Device names are lowercase, as returned by `XRSettings.supportedDevices`.
string desiredDevice = "daydream"; // Or "cardboard".
// Some VR Devices do not support reloading when already active, see
// https://docs.unity3d.com/ScriptReference/XR.XRSettings.LoadDeviceByName.html
if (String.Compare(XRSettings.loadedDeviceName, desiredDevice, true) != 0) {
XRSettings.LoadDeviceByName(desiredDevice);
// Must wait one frame after calling `XRSettings.LoadDeviceByName()`.
yield return null;
}
// Now it's ok to enable VR mode.
XRSettings.enabled = true;
}
Switching to 2D mode at runtime
To leave VR mode at runtime, load the "None" device, wait one frame, then reset
camera transform and settings. Here is an example:
// Call via `StartCoroutine(SwitchTo2D())` from your code. Or, use
// `yield SwitchTo2D()` if calling from inside another coroutine.
IEnumerator SwitchTo2D() {
// Empty string loads the "None" device.
XRSettings.LoadDeviceByName("");
// Must wait one frame after calling `XRSettings.LoadDeviceByName()`.
yield return null;
// Not needed, since loading the None (`""`) device takes care of this.
// XRSettings.enabled = false;
// Restore 2D camera settings.
ResetCameras();
}
// Resets camera transform and settings on all enabled eye 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 position.
// Only required if you change the camera's local position while in 2D mode.
cam.transform.localPosition = Vector3.zero;
// Reset local rotation.
// Only required if you change the camera's local rotation while in 2D mode.
cam.transform.localRotation = Quaternion.identity;
// No longer needed, see issue github.com/googlevr/gvr-unity-sdk/issues/628.
// cam.ResetAspect();
// No need to reset `fieldOfView`, since it's reset automatically.
}
}
}
Detecting whether your app was launched from VR
Daydream apps can be launched from VR or from the 2D launcher. See the
Android Manifest for Google VR
reference for a list of supported Android intent filters that enable launching
from VR or 2D.
Unity ensures that hybrid Daydream apps launched from VR automatically start in
VR mode, even if the None device is listed above the Daydream VR device
in Player Settings > Android/iOS > Virtual Reality SDKs.
This helps ensure that your app meets Daydream design requirement
UX-D9.
To detect whether your app was launched from VR at runtime, use
GvrIntent.IsLaunchedFromVr()
.
This method returns true
if the Android intent
that launched your app contains the android.intent.extra.VR_LAUNCH
extra,
indicating that your app was launched from VR.
All rights reserved. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-10-09 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-10-09 UTC."],[[["\u003cp\u003eHybrid apps can support both 2D and VR modes, allowing users to switch between them during runtime.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can configure the app to launch in either 2D or VR mode initially, with Daydream apps required to start in VR when launched from Daydream home.\u003c/p\u003e\n"],["\u003cp\u003eSwitching between modes involves loading the desired device (None for 2D, Daydream or Cardboard for VR), waiting a frame, and then enabling/disabling VR mode and resetting camera settings as needed.\u003c/p\u003e\n"],["\u003cp\u003eDetecting the launch environment (VR or 2D) can be done using \u003ccode\u003eGvrIntent.IsLaunchedFromVr()\u003c/code\u003e to ensure a seamless user experience.\u003c/p\u003e\n"]]],["Hybrid apps can switch between 2D and VR modes. Developers configure the initial mode in **Player Settings** by ordering \"None,\" \"Daydream,\" or \"Cardboard\" in **Virtual Reality SDKs**. To switch at runtime, use `XRSettings.LoadDeviceByName()` with the desired device name (\"daydream,\" \"cardboard,\" or \"\" for None), wait one frame, and enable or disable VR mode via `XRSettings.enabled`. Reset camera settings when exiting VR mode. Daydream apps launched from VR automatically start in VR mode and `GvrIntent.IsLaunchedFromVr()` detects if the app launched from VR.\n"],null,["Hybrid apps support both 2D (non-VR) and VR modes.\n\n- Developers can choose whether a hybrid app starts in 2D or in VR mode.\n However, Daydream apps must be configured to start in VR mode when launched\n from Daydream home.\n\n- Developers are responsible for providing users a way to switch\n between modes.\n\n- While in 2D, apps can optionally allow users to preview content inside a\n [Magic Window](/vr/develop/unity/guides/magic-window) view, which may\n or may not be full screen.\n\nPrerequisites\n\nTo enable switching between 2D and VR at runtime in your app:\n\n1. Ensure **Player Settings \\\u003e Android/iOS \\\u003e Virtual Reality Supported** is\n checked.\n\n2. Add the following entries in **Player Settings \\\u003e Android/iOS \\\u003e Virtual\n Reality SDKs**:\n\n - Add **None** to support 2D mode in your app.\n\n - Add **Daydream** and/or **Cardboard** to support VR mode in your app.\n\n3. Order the SDK options to control which mode your app starts in when\n launched from the 2D desktop launcher:\n\n - To start in 2D mode, put **None** first.\n\n - To start in VR mode, put **Daydream** or **Cardboard** first.\n\nSwitching to VR mode at runtime\n\nTo enter VR mode at runtime, load the desired VR device, wait one frame, then\nenable VR mode. Here is an example: \n\n // Call via `StartCoroutine(SwitchToVR())` from your code. Or, use\n // `yield SwitchToVR()` if calling from inside another coroutine.\n IEnumerator SwitchToVR() {\n // Device names are lowercase, as returned by `XRSettings.supportedDevices`.\n string desiredDevice = \"daydream\"; // Or \"cardboard\".\n\n // Some VR Devices do not support reloading when already active, see\n // https://docs.unity3d.com/ScriptReference/XR.XRSettings.LoadDeviceByName.html\n if (String.Compare(XRSettings.loadedDeviceName, desiredDevice, true) != 0) {\n XRSettings.LoadDeviceByName(desiredDevice);\n\n // Must wait one frame after calling `XRSettings.LoadDeviceByName()`.\n yield return null;\n }\n\n // Now it's ok to enable VR mode.\n XRSettings.enabled = true;\n }\n\nSwitching to 2D mode at runtime\n\nTo leave VR mode at runtime, load the \"None\" device, wait one frame, then reset\ncamera transform and settings. Here is an example: \n\n // Call via `StartCoroutine(SwitchTo2D())` from your code. Or, use\n // `yield SwitchTo2D()` if calling from inside another coroutine.\n IEnumerator SwitchTo2D() {\n // Empty string loads the \"None\" device.\n XRSettings.LoadDeviceByName(\"\");\n\n // Must wait one frame after calling `XRSettings.LoadDeviceByName()`.\n yield return null;\n\n // Not needed, since loading the None (`\"\"`) device takes care of this.\n // XRSettings.enabled = false;\n\n // Restore 2D camera settings.\n ResetCameras();\n }\n\n // Resets camera transform and settings on all enabled eye cameras.\n void ResetCameras() {\n // Camera looping logic copied from GvrEditorEmulator.cs\n for (int i = 0; i \u003c Camera.allCameras.Length; i++) {\n Camera cam = Camera.allCameras[i];\n if (cam.enabled && cam.stereoTargetEye != StereoTargetEyeMask.None) {\n\n // Reset local position.\n // Only required if you change the camera's local position while in 2D mode.\n cam.transform.localPosition = Vector3.zero;\n\n // Reset local rotation.\n // Only required if you change the camera's local rotation while in 2D mode.\n cam.transform.localRotation = Quaternion.identity;\n\n // No longer needed, see issue github.com/googlevr/gvr-unity-sdk/issues/628.\n // cam.ResetAspect();\n\n // No need to reset `fieldOfView`, since it's reset automatically.\n }\n }\n }\n\nDetecting whether your app was launched from VR\n\nDaydream apps can be launched from VR or from the 2D launcher. See the\n[Android Manifest for Google VR](/vr/reference/vr-manifest#platform_compatibility)\nreference for a list of supported Android intent filters that enable launching\nfrom VR or 2D.\n\nUnity ensures that hybrid Daydream apps launched from VR automatically start in\nVR mode, even if the **None** device is listed above the **Daydream** VR device\nin **Player Settings \\\u003e Android/iOS \\\u003e Virtual Reality SDKs** .\nThis helps ensure that your app meets Daydream design requirement\n[UX-D9](/vr/distribute/daydream/design-requirements#UX-D9).\n\nTo detect whether your app was launched from VR at runtime, use\n[`GvrIntent.IsLaunchedFromVr()`](/vr/reference/unity/class/GvrIntent#classGvrIntent_1a66212d583253e0dca3bd00cc563d3739).\nThis method returns `true` if the Android intent\nthat launched your app contains the `android.intent.extra.VR_LAUNCH` extra,\nindicating that your app was launched from VR."]]