[VR] Properly "Load if available" VR System
Proper "Load if available" VR System
TL;DR: Using Unity's Native VR API [5.6.0f3] returns an error when using OpenVR and no devices are connected.
The Situation
We developers need to test the environment oftenly, and sometimes in a VR environment, not everyone has access to the VR Devices, and may test without it. So far, we were trying to develop a system where we could test with or without a VR Device, by controlling inputs with keyboard and mouse instead.
The Problem
The problem is that if we don't have any VR Devices attached, OpenVR throws an error that cannot be handled.
The Current Setup
Our current setup includes the following logic:
void Awake()
{
StartCoroutine(TryLoadVR());
}
IEnumerator TryLoadVR()
{
// This will return a list with Oculus and OpenVR
string[] devices = new string[vrDevices.Length];
for (int i = 0; i < devices.Length; i++)
devices[i] = vrDevices[i].deviceName;
// This line if no VR Device is connected returns an error with OpenVR
VRSettings.LoadDeviceByName(devices);
yield return null;
if (string.IsNullOrEmpty(VRSettings.loadedDeviceName))
{
// Load Non-VR Input Controls
}
else
{
// Load VR Input Controls
}
}
I have checked Virtual Reality Supported on the Player Settings, and on the list of Virtual Reality Supported SDKs I have on the order:
None
Oculus
OpenVR
The error I'm getting is:
VR: OpenVR Error! OpenVR failed with initialization with error code VRInitError_Init_HmdNotFoundPresenceFailed: "Hmd Not Found Presence Failed (126)"!
I have tried using try catch but it doesn't work to isolate the error, and if it does throw the error, it keeps throwing even after stopping the game application on the editor.
Answer by joaoborks · Jun 14, 2017 at 06:12 PM
Just found out that by adding: VRSettings.LoadDeviceByName("None")
if Unity cannot load the VR Device, can keep the game running smoothly after the OpenVR error returned, yet we can't use the Error Pause to track errors, or manually need to enable it each time we play or unpause the game after the startup OpenVR Error which is a little annoying.
Full Code:
void Awake()
{
StartCoroutine(TryLoadVR());
}
IEnumerator TryLoadVR()
{
string[] devices = new string[vrDevices.Length];
for (int i = 0; i < devices.Length; i++)
devices[i] = vrDevices[i].deviceName;
VRSettings.LoadDeviceByName(devices);
yield return null;
if (string.IsNullOrEmpty(VRSettings.loadedDeviceName))
{
// Load Non-VR Input Controls
// Stops trying to load something that is not there
VRSettings.LoadDeviceByName("None");
}
else
{
// Load VR Input Controls
VRSettings.enabled = true;
}
}
EDIT: Uploaded a public gist to help others with the same difficulties
I'm currently working on the same thing (Unity 2017.3) and can't get it to work properly - no matter what order I use for the VR SD$$anonymous$$s (Edit - Project Settings - Player - XR Settings):
OpenVR, then None: If no device is connected, it throws an "HmdNotFound" error (not sure how or if we can catch it). I tried checking if there's an H$$anonymous$$D with
if(XRDevice.isPresent)
and then loading the according SD$$anonymous$$ (e.g.XRSettings.LoadDeviceByName("None")
) in the "Awake" function of the very first script in the very first scene that's loaded but it still throws the same error message.None, then OpenVR: No error message when no H$$anonymous$$D is connected but if there is, Unity simply doesn't see it (`XRDevice.isPresent` always returns "false") and S$$anonymous$$mVR/OpenVR don't start properly either. So in the end my app runs in non-VR mode, even though there's a device connected.