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 _= vrDevices*.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:
1. None
2. Oculus
3. 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.