Closed CliveDent closed 5 years ago
Turned out I had written some overly complicated code in my custom VR Manager which only worked on Windows. In case anybody finds it helpful, here is the working version:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
public class VRManager : MonoBehaviour
{
public const string DEVICE_NONE = "None";
public const string DEVICE_OPEN_VR = "OpenVR";
public const string DEVICE_OCULUS = "Oculus";
public const string DEVICE_STEREO = "stereo";
public const string DEVICE_CARDBOARD = "cardboard";
public bool forceOff;
private void Start()
{
this.ApplySettings();
}
public string[] GetSupportedDevices()
{
string[] supportedDevices;
if( Application.platform == RuntimePlatform.Android )
{
supportedDevices = new string[] { DEVICE_CARDBOARD };
}
else if( Application.platform == RuntimePlatform.WindowsPlayer )
{
supportedDevices = new string[] { DEVICE_OPEN_VR, DEVICE_OCULUS, DEVICE_STEREO };
}
else
{
List<string> devices = new List<string>( XRSettings.supportedDevices );
devices.Remove( DEVICE_NONE );
supportedDevices = devices.ToArray();
}
return supportedDevices;
}
public void ApplySettings()
{
if( forceOff )
{
if( XRSettings.enabled )
{
this.LoadDevice( VRManager.DEVICE_NONE );
}
}
else
{
bool vrModeOn = PlayerPrefs.GetInt( Preferences.VR_ENABLED ) > 0;
StartCoroutine( ToggleVRCoroutine( vrModeOn ) );
}
}
public void ToggleVR()
{
StartCoroutine( ToggleVRCoroutine( !XRSettings.enabled ) );
}
public void LoadDevice( string deviceName )
{
StartCoroutine( LoadDeviceCoroutine( deviceName ) );
}
private IEnumerator LoadDeviceCoroutine( string deviceName )
{
yield return null;
if( String.Compare( XRSettings.loadedDeviceName, deviceName, true ) == 0 )
{
Debug.Log( "<b>[VR Manager]</b> Already using device '" + deviceName + "'." );
}
else
{
Debug.Log( "<b>[VR Manager]</b> Switching to VR device '" + deviceName + "'." );
XRSettings.LoadDeviceByName( deviceName );
yield return null;
XRSettings.enabled = String.Compare( XRSettings.loadedDeviceName, DEVICE_NONE ) != 0;
}
}
private IEnumerator ToggleVRCoroutine( bool enable )
{
if( XRSettings.enabled == enable )
{
Debug.Log( "<b>[VR Manager]</b> Already " + ( enable ? "on" : "off" ) + "." );
}
else
{
Debug.Log( "<b>[VR Manager]</b> Turning " + ( enable ? "on" : "off" ) + "." );
if( enable )
{
XRSettings.LoadDeviceByName( this.GetSupportedDevices() );
yield return null;
XRSettings.enabled = true;
yield return null;
Debug.Log( "<b>[VR Manager]</b> Loaded device '" + XRSettings.loadedDeviceName + "'." );
}
else
{
if( String.Compare( XRSettings.loadedDeviceName, DEVICE_NONE, true ) != 0 )
{
XRSettings.LoadDeviceByName( DEVICE_NONE );
yield return null;
XRSettings.enabled = false;
}
}
}
}
public void Recenter()
{
InputTracking.Recenter();
Debug.Log( "<b>[VR Manager]</b> Recentered." );
}
}
@CliveDent what is the difference in the two versions of code you had? I have a fairly simple switch to VR coroutine that is causing this same issue.
public class SceneController : MonoBehaviour {
public void ChangeScene() {
if (!XRSettings.enabled) {
StartCoroutine(SwitchToVR());
}
}
private IEnumerator SwitchToVR() {
string desiredDevice = "daydream";
if (System.String.Compare(XRSettings.loadedDeviceName, desiredDevice, true) != 0) {
XRSettings.LoadDeviceByName(desiredDevice);
yield return null;
XRSettings.enabled = true;
}
SceneManager.LoadScene("Loading");
}
}
If I remember it correctly I had another condition to check if the device was loaded before setting enabled to true. The posted version worked and so should yours. Maybe your problems is somewhere else.
I don't know what your project looks like but it seems to me, that enabling VR in constructor is not necessary because you can simply change priority of VR devices in your project settings so "none" is not first.
Also, in Unity, constructors are rarely used. Maybe if you move the StartCoroutine to Start method it will work. But I'm just guessing, I only just started with Unity.
Thanks for your detailed response @CliveDent, I managed to fix this error by updating to the latest version of the gvr-unity-sdk
(now I'm getting a new error, later on, though that's a new issue)
Just leaving my notes here as I had the same issue after I upgraded to Unity 2019.2.2f1. I managed to get GoogleVR working again after I updated/installed it through the Unity Package Manager (Window->Package Manager), which got it working again once I built to Android :-)
I can't switch to Cardboard view. It used to work and then it stopped for some reason. I googled it and some people were able to solve it by updating their packages or editing their manifest file. Not me.
SPECIFIC ISSUE ENCOUNTERED
I have latest GoogleVR package but when I try to enable VR in my Android app, it says this:
Google VR Error [null]: Exception initializing GoogleVR from Unity library. Didn't find class "com.unity3d.unitygvr.GoogleVR" on path: DexPathList[[zip file "/data/app/###==/base.apk"],nativeLibraryDirectories=[/data/app/###==/lib/arm, /data/app/###base.apk!/lib/armeabi-v7a, /system/lib, /system/vendor/lib]]
(I have replaced my app package name with ###)
What have I tried:
HARDWARE/SOFTWARE VERSIONS
STEPS TO REPRODUCE THE ISSUE