前一篇已将SDK和触发事件写好,这里主要写转换模式的代码
1.VR模式转为非VR模式
首先要引用using UnityEngine.EventSystems;命名空间
将VR的部分东西设为False,转换模式后相机还是VR模式,需要更新相机
public void VR_Off()
{
//Cam_PHYRAY.enabled = true;
VREventSys.SetActive(false);
UIEventSys.SetActive(true);
GvrReticlePointer.SetActive(false);
StartCoroutine(VROFF());
UI_2D.SetActive(true);
UI_3D.SetActive(false);
yuan_dian.SetActive(false);
}
IEnumerator VROFF()
{
UnityEngine.XR.XRSettings.LoadDeviceByName("");
yield return null;
ResetCameras();
}
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 aspect ratio based on normal (non-VR) screen size.
cam.ResetAspect();
// Don't need to reset camera `fieldOfView`, since it's restored to the original value automatically.
}
}
}
2 非VR模式转为VR模式
public void VR_on()
{
//Cam_PHYRAY.enabled = false;
VREventSys.SetActive(true);
UIEventSys.SetActive(false);
GvrReticlePointer.SetActive(true);
StartCoroutine(VRON());
UI_2D.SetActive(false);
UI_3D.SetActive(true);
yuan_dian.SetActive(true);
}
{
//Cam_PHYRAY.enabled = false;
VREventSys.SetActive(true);
UIEventSys.SetActive(false);
GvrReticlePointer.SetActive(true);
StartCoroutine(VRON());
UI_2D.SetActive(false);
UI_3D.SetActive(true);
yuan_dian.SetActive(true);
}
IEnumerator VRON()
{
UnityEngine.XR.XRSettings.LoadDeviceByName("cardboard");
{
UnityEngine.XR.XRSettings.LoadDeviceByName("cardboard");
yield return null;
UnityEngine.XR.XRSettings.enabled = true;
}
UnityEngine.XR.XRSettings.enabled = true;
}
基本一样