最近Unity3D的版本更新到2018版本,再用到很多以前插件和功能开发的时候,遇到很多问题,其中在做Google Cardboard开发的时候,本来想用最新的SDK开发,但是用最新的Unity发现在做全景模式和VR模式的时候,竟然没有了这个功能。网上搜了搜,原因是Cardboard整合到Unity里面去了,但是整合之后就没有了原来的控制VR开关的VRModeEnabled = 布尔值的参数。所以我在网上找到方法可以解决这个问题。
下面我把最新的Unity版本的方法贴出来,老版本的可以用GvrViewer里面的VRModeEnabled = 布尔值,来控制。如果不想用陀螺仪来控制方向,可以用GvrViewer.Controller.Head.trackRotation = 布尔值来控制。文章源自大腿Plus-https://www.zhaoshijun.com/archives/986
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
using System.Collections; using UnityEngine; using UnityEngine.XR; public class VRViewManager : MonoBehaviour { public bool VRModeEnabled = true; private float x = 0, y = 0; // Use this for initialization void Start() { SetMode(VRModeEnabled); } void Update() { #if UNITY_ANDROID && !UNITY_EDITOR // Exit when (X) is tapped. if (Input.GetKeyDown(KeyCode.Escape)) { //Application.Quit(); if (VRModeEnabled) { SetMode(false); } } #endif if (!VRModeEnabled) { #region Mouse&TouchEvent float x = Input.GetAxis("Mouse X"); float y = Input.GetAxis("Mouse Y"); Vector2 deltaPosition = new Vector2(x, y); #if UNITY_STANDALONE_WIN || UNITY_EDITOR if (Input.GetMouseButtonDown(0)) { MouseOrTouchDown(Input.mousePosition); } if (Input.GetMouseButton(0)) { MouseOrTouch(Input.mousePosition, deltaPosition); } if (Input.GetMouseButtonUp(0)) { MouseOrTouchUp(Input.mousePosition); } #endif #if UNITY_ANDROID || UNITY_IPHONE if (Input.touchCount > 0) { if (Input.GetTouch(0).phase == TouchPhase.Began) { MouseOrTouchDown(Input.GetTouch(0).position); } if (Input.GetTouch(0).phase == TouchPhase.Moved) { MouseOrTouch(Input.GetTouch(0).position, deltaPosition); } if (Input.GetTouch(0).phase == TouchPhase.Ended) { MouseOrTouchUp(Input.GetTouch(0).position); } } #endif #endregion } } #region Mouse&TouchEvent /// <summary> /// 鼠标或者手指按下事件 /// </summary> /// <param name="position"></param> private void MouseOrTouchDown(Vector3 position) { } /// <summary> /// 鼠标或者手指触摸按住事件 /// </summary> /// <param name="position"></param> /// <param name="deltaPosition"></param> private void MouseOrTouch(Vector3 position, Vector2 deltaPosition) { x -= deltaPosition.y; x = Mathf.Clamp(x, -45, 45); y -= deltaPosition.x; transform.localEulerAngles = new Vector3(-x, y, 0); } /// <summary> /// 鼠标或者手指抬起事件 /// </summary> /// <param name="position"></param> private void MouseOrTouchUp(Vector3 position) { } #endregion public void SetMode(bool enabled) { VRModeEnabled = enabled; string deviceName = enabled ? XRSettings.supportedDevices[1] : XRSettings.supportedDevices[0]; StartCoroutine(ModeVR360(enabled, deviceName)); } IEnumerator ModeVR360(bool enabled, string deviceName) { Debug.Log("Mode = " + deviceName); yield return new WaitUntil(() => enabled ? Run(deviceName) : Run(enabled)); yield return new WaitUntil(() => XRSettings.loadedDeviceName == deviceName); yield return new WaitUntil(() => enabled ? Run(enabled) : Run(deviceName)); } bool Run(string deviceName) { XRSettings.LoadDeviceByName(deviceName); return true; } bool Run(bool enabled) { XRSettings.enabled = enabled; return true; } } |
我用的是2018版本,5.6以后的版本就已经整合了,但是用的是VRSetting,2018版本的弃用了VRSetting改用XRSetting,还有就是我上面加了关掉VR模式后,可以滑动改变摄像机角度,进行全景观看。文章源自大腿Plus-https://www.zhaoshijun.com/archives/986
只要将PlayerSetting里面的Virtual Reality Supported勾上,选择Cardboard和一个None。这个必须要选择一个None,否则是切换不了的。而且对于我这个代码来说None是放在第一个的。文章源自大腿Plus-https://www.zhaoshijun.com/archives/986 文章源自大腿Plus-https://www.zhaoshijun.com/archives/986
![weinxin](https://www.zhaoshijun.com/wp-content/uploads/2021/12/wechat.png)
![weinxin](https://www.zhaoshijun.com/wp-content/uploads/2023/11/qq.jpg)
评论