I’m experiencing an issue with rendering in my Unity Android application. When I toggle between Cardboard VR and normal views using the following code:
using UnityEngine;
using UnityEngine.XR;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
public class VRToggle : MonoBehaviour
{
private bool vrEnabled = false;
public Camera mainCamera;
private void Start()
{
// Disable XR settings on start
StopCardboard();
}
public void ToggleVR()
{
if (vrEnabled)
{
Debug.Log("Prendido");
StartCoroutine(StopCardboard());
}
else
{
Debug.Log("Apagado");
StartCoroutine(StartCardboard());
}
}
private void EnableVR()
{
string[] vrDeviceNames = XRSettings.supportedDevices;
foreach (string deviceName in vrDeviceNames)
{
if (deviceName.ToLower().Contains("cardboard"))
{
XRSettings.LoadDeviceByName(deviceName);
break;
}
}
XRSettings.enabled = true;
vrEnabled = true;
Debug.Log("Enable VR");
}
private void DisableVR()
{
XRSettings.LoadDeviceByName("None");
XRSettings.enabled = false;
vrEnabled = false;
Debug.Log("Disable VR");
}
public IEnumerator StopCardboard()
{
Debug.Log("Apagando");
string deviceName = "Cardboard";
// Obtiene una lista de subsistemas XR disponibles
List<XRDisplaySubsystem> displaySubsystems = new List<XRDisplaySubsystem>();
SubsystemManager.GetSubsystems(displaySubsystems);
// Busca el subsistema XR con el nombre especificado
XRDisplaySubsystem cardboardSubsystem = null;
foreach (var subsystem in displaySubsystems)
{
if (subsystem.SubsystemDescriptor.id.Contains(deviceName))
{
cardboardSubsystem = subsystem;
break;
}
}
if (cardboardSubsystem == null)
{
Debug.Log("Inicio app: No se encontro ningun XR llamado:" + deviceName);
Screen.orientation = ScreenOrientation.AutoRotation;
}
else
{
//Detiene cardboard
cardboardSubsystem.Stop();
ResetCameras();
Screen.orientation = ScreenOrientation.LandscapeLeft;
}
vrEnabled = false;
Debug.Log("STOP VR");
yield return null;
}
public IEnumerator StartCardboard()
{
Debug.Log("Prendiendo");
string deviceName = "Cardboard";
// Obtiene una lista de subsistemas XR disponibles
List<XRDisplaySubsystem> displaySubsystems = new List<XRDisplaySubsystem>();
SubsystemManager.GetSubsystems(displaySubsystems);
// Busca el subsistema XR con el nombre especificado
XRDisplaySubsystem cardboardSubsystem = null;
foreach (var subsystem in displaySubsystems)
{
if (subsystem.SubsystemDescriptor.id.Contains(deviceName))
{
cardboardSubsystem = subsystem;
break;
}
}
if (cardboardSubsystem == null)
{
Debug.Log("No se encontro ningun XR llamado:" + deviceName);
Screen.orientation = ScreenOrientation.AutoRotation;
}
else
{
//Detiene cardboard
cardboardSubsystem.Start();
ResetCameras();
Screen.orientation = ScreenOrientation.LandscapeLeft;
}
vrEnabled = true;
Debug.Log("START VR");
yield return null;
}
void ResetCameras()
{
// Camera looping logic copied from GvrEditorEmulator.cs
//for (int i = 0; i < Camera.allCameras.Length; i++)
//{
//Camera cam = mainCamera;
if (mainCamera.enabled && mainCamera.stereoTargetEye != StereoTargetEyeMask.None)
{
Debug.Log("Dentro del IF!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
// Reset local position.
// Only required if you change the camera's local position while in 2D mode.
mainCamera.transform.localPosition = Vector3.zero;
// Reset local rotation.
// Only required if you change the camera's local rotation while in 2D mode.
mainCamera.transform.localRotation = Quaternion.identity;
// No longer needed, see issue github.com/googlevr/gvr-unity-sdk/issues/628.
mainCamera.ResetAspect();
// No need to reset `fieldOfView`, since it's reset automatically.
mainCamera.Reset();
Debug.Log(mainCamera.actualRenderingPath);
}
//}
}
public void Update()
{
// Check for input to toggle VR mode
if (Input.GetButtonDown("Crawl"))
{
ToggleVR();
}
}
}
The problem arises when I exit Cardboard VR mode and switch back to the normal view. At that point, my scene rendering seems to stop, and all I can see is the skybox and some floating windows. What’s curious is that my character retains its functionality, and I can navigate through this “invisible” scene. Additionally, when I switch back to Cardboard VR mode, everything appears in its correct place, and all physics and colliders function correctly.
This issue only occurs when testing on an Android device. The Unity Editor’s preview doesn’t replicate this problem.
I’m not modifying any camera properties other than resetting it once VR mode is toggled. I’m using Unity 2021.3.
Note: This only happens on android device, i can´t reproduce the bug on runtime of unity or on preview.
I would appreciate any insights or suggestions from the Unity community on how to resolve this rendering issue when switching between VR and normal modes on Android.
Thank you in advance for your help!