Unity教程合集Unity技术VR/AR分享

Unity对于VR的支持

2017-05-24  本文已影响0人  GreatSun

如何打开Unity对于VR的支持

在edit下拉菜单里,找到Project Setting选项,然后选择Player,在Inspector中找到Other Setting,勾选Virtual Reality Supported复选框,即可获取对于VR设备的支持。

VR支持

点击+号即可添加新的设备。目前,Unity支持的设备有oculus,openVR, Ste reo Display, Split Stereo Display。后面两个是3D显示器。

支持产品

其中None的作用是可以在初始化的过程中,不进行VR头显的渲染,这样可以让同一个程序对多个设置进行支持。
下面我们来介绍下如何使用脚本来进行支持的SDK种类的切换。

图1
    IEnumerator LoadDevice(string newDevice)
    {
        yield return new WaitForSeconds(3.0f);

        VRSettings.LoadDeviceByName(newDevice);
        yield return null;
        VRSettings.enabled = true;
    }

为了以示区别,我们在更改支持的SDK前先等待3秒,在此时间内,只有电脑屏幕有画面,3s过后,我们可以看到VIVE头盔被激活。此时应注意在enable前,需等待一帧,否则会报错。

相机设置

在添加了VR支持后,我们会发现在camera的Inspector面板中多出如图2所示的几个选项。

图2

Unity对手柄的支持

手柄的位置信息

InputTracking类可以用于获取VR节点的位置和旋转角度。其官方的解释如下:

    //
    // Summary:
    //     ///
    //     VR Input tracking data.
    //     ///
    public static class InputTracking
    {
        //
        // Summary:
        //     ///
        //     The current position of the requested VRNode.
        //     ///
        //
        // Parameters:
        //   node:
        //     Node index.
        //
        // Returns:
        //     ///
        //     Position of node local to its tracking space.
        //     ///
        public static Vector3 GetLocalPosition(VRNode node);
        //
        // Summary:
        //     ///
        //     The current rotation of the requested VRNode.
        //     ///
        //
        // Parameters:
        //   node:
        //     Node index.
        //
        // Returns:
        //     ///
        //     Rotation of node local to its tracking space.
        //     ///
        public static Quaternion GetLocalRotation(VRNode node);
        //
        // Summary:
        //     ///
        //     Center tracking to the current position and orientation of the HMD.
        //     ///
        public static void Recenter();
    }

以下,我们通过一个简单的示例来演示如何使用InputTracking类来模拟手柄。

        this.transform.localPosition = InputTracking.GetLocalPosition(VRNode.RightHand);
        this.transform.localRotation = InputTracking.GetLocalRotation(VRNode.RightHand);

LeftHand脚本中做出相应的替换即可。

运行程序,我们就可以看到手柄变成了两个cube。

手柄的按键

HTC VIVE手柄的按键可以在Edit->Project Setting->Input中设定。
首先,我们需要找到HTC VIVE手柄对应的ID号,在unity的官网上可以找到其对应的信息:

https://docs.unity3d.com/Manual/OpenVRControllers.html
图3 图4

此时,我们就可以在Input里面设置手柄对应的按键了。我们以手柄上的trigger键为例,介绍如何使用手柄。

图5

其中positive button对应即是上表中Unity Button ID中对应的值。

      if (Input.GetButton("LCT"))
        {
            Debug.Log("LCT");
        }

        if (Input.GetButton("RCT"))
        {
            Debug.Log("RCT");
        }

如果我们想获得trigger按下去的程度的数值,则可以使用Squeeze类型。
具体做法如下:

图6

与之前不同的地方是,Axis要改为9th axis,type要改为Joystick Axis类型。

float f1 = Input.GetAxis("LCT");
if (f1 > 0.1f)
{
      Debug.Log(f1);
}

此时,我们可以做一个射线放置到手柄上。

图7
    public void Do()
    {
        Debug.Log("Press the button");
    }
图8
        Ray ray = new Ray(transform.position, transform.forward);
        RaycastHit hit;

        if (Input.GetButtonDown("LCT"))
        {
            if (Physics.Raycast(ray, out hit, 10000))
            {
                Debug.Log(hit.transform.name);
                hit.transform.GetComponent<UnityEngine.UI.Button>().OnSubmit(new BaseEventData(EventSystem.current));

            }
        }

使用这种方法对于UI的通用性比较差,如果我们在添加toggle或是其他UI组件,就要重新的GetComponent,比较麻烦,为此,我们可以采取以下的做法。

    public void Do2()
    {
        Debug.Log("Press the toggle");
    }
}
图9
        Ray ray = new Ray(transform.position, transform.forward);
        RaycastHit hit;

        if (Input.GetButtonDown("LCT"))
        {
            if (Physics.Raycast(ray, out hit, 10000))
            {
                Debug.Log(hit.transform.name);
                // hit.transform.GetComponent<UnityEngine.UI.Button>().OnSubmit(new BaseEventData(EventSystem.current));
                ExecuteEvents.Execute<ISubmitHandler>(hit.transform.gameObject, new BaseEventData(EventSystem.current), ExecuteEvents.submitHandler);

            }
        }
上一篇 下一篇

猜你喜欢

热点阅读