我是用daydream平台测试的,目前别的平台还没有测试
大概思路是playerSettings 中设置勾选Virtual Reality Supported 后选着对应的平台这里选的是Daydream
然后在转换平台时用下面的方法加载到对应的平台,
UnityEngine.XR.XRSettings.LoadDeviceByName("None")
UnityEngine.XR.XRSettings.LoadDeviceByName("daydream");
并设置UnityEngine.XR.XRSettings.enabled = true;就可以切换到vr模式了
在同一个场景中增加两个MainCamera,
下面把脚本附上
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.XR; public class Manger3dTo2d : MonoBehaviour { private bool is3d = true; public GameObject canvas; public GameObject mainCamare; public GameObject camare; //public GameObject evenSystem; public TextMesh tm; //public GameObject hand;// Use this for initializationvoid Start (){ tm.text = XRSettings.loadedDeviceName; Debug.Log(XRSettings.loadedDeviceName + "333"); is3d = true;canvas.SetActive(false); camare.SetActive(false);}// Update is called once per framevoid Update () { if ((GvrControllerInput.AppButtonUp || Input.GetKeyUp(KeyCode.A)) && is3d) { canvas.SetActive(true); is3d = false; //UnityEngine.XR.XRSettings.enabled = false; XRSettings.LoadDeviceByName("None"); Debug.Log(XRSettings.loadedDeviceName); tm.text = XRSettings.loadedDeviceName; //evenSystem.GetComponent<GvrPointerInputModule>().enabled = false; //evenSystem.GetComponent<StandaloneInputModule>().enabled = true; mainCamare.SetActive(false); camare.SetActive(true); //hand.SetActive(false); } if (GvrControllerInput.ClickButtonDown || Input.GetKeyUp(KeyCode.B)) { To3d(); }} public void To3d() { StartCoroutine(VrOn()); } IEnumerator VrOn() { XRSettings.LoadDeviceByName("daydream"); Debug.Log(XRSettings.loadedDeviceName + "1111"); #if UNITY_EDITOR #endif #if UNITY_ANDROID yield return new WaitUntil(() => XRSettings.loadedDeviceName == "daydream");//到这一步pc不能测试了 #endif Debug.Log(XRSettings.loadedDeviceName + "222"); yield return null; XRSettings.enabled = true; Debug.Log(XRSettings.loadedDeviceName); tm.text = XRSettings.loadedDeviceName; canvas.SetActive(false); is3d = true; mainCamare.SetActive(true); camare.SetActive(false); //hand.SetActive(true); //evenSystem.GetComponent<GvrPointerInputModule>().enabled = true; //evenSystem.GetComponent<StandaloneInputModule>().enabled = false; } public void ButtonClick() { Debug.Log("这是2d场景"); } }