Unity3D:谷歌Cardboard全景模式和VR模式切换

最近Unity3D的版本更新到2018版本,再用到很多以前插件和功能开发的时候,遇到很多问题,其中在做Google Cardboard开发的时候,本来想用最新的SDK开发,但是用最新的Unity发现在做全景模式和VR模式的时候,竟然没有了这个功能。网上搜了搜,原因是Cardboard整合到Unity里面去了,但是整合之后就没有了原来的控制VR开关的VRModeEnabled = 布尔值的参数。所以我在网上找到方法可以解决这个问题。文章源自大腿Plus-https://www.shijunzh.com/archives/986

下面我把最新的Unity版本的方法贴出来,老版本的可以用GvrViewer里面的VRModeEnabled = 布尔值,来控制。如果不想用陀螺仪来控制方向,可以用GvrViewer.Controller.Head.trackRotation = 布尔值来控制。文章源自大腿Plus-https://www.shijunzh.com/archives/986

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.shijunzh.com/archives/986

只要将PlayerSetting里面的Virtual Reality Supported勾上,选择Cardboard和一个None。这个必须要选择一个None,否则是切换不了的。而且对于我这个代码来说None是放在第一个的。文章源自大腿Plus-https://www.shijunzh.com/archives/986

大腿Plus
  • 本文由 发表于 2018年8月20日14:44:34
  • 转载请务必保留本文链接:https://www.shijunzh.com/archives/986

发表评论