2017年3月24日修改编程
vive的sdk SteamVR在升级,会有些改动。最新的SteamVR1.2.1在Unity5.4.4下狂报错。.net
下面的内容是在如下环境完成的插件
unity5.4.43d
SteamVR1.2.0(http://download.csdn.net/detail/wuyt2008/9792970)code
ViveInputUtility1.5.1beta(http://download.csdn.net/detail/wuyt2008/9792980)blog
啊,为何开始作vive的开发了,这个问题别问哦。好了,说正事。游戏
请看下图事件
手柄两个一对,分左右,开发的时候也是分左右的。每一个上面有一个pad和4个按钮。简单说明下:ip
须要两个插件:开发
SteamVR Plugin和
Vive Input Utility
商城地址以下,都是免费的,下载后导入
https://www.assetstore.unity3d.com/cn/#!/content/32647
https://www.assetstore.unity3d.com/cn/#!/content/64219
using HTC.UnityPlugin.Vive;
每一个按钮包括pad都有GetPress、GetPressDown、GetPressUp三种方法,用HandRole枚举来肯定左右手柄,用ControllerButton枚举来肯定是哪一个按钮。
对于按钮,GetPressDown是按下时触发,GetPressUp是放开时触发,以上两个是个事件,GetPress是按住时一直返回ture,算是一个状态。
对于pad,有两种:
当ControllerButton.Pad时,和按钮相同。
当ControllerButton.PadTouch时,GetPressDown是接触时触发,GetPressUp是离开时触发,GetPress是接触时一直返回的状态。
using System.Collections; using System.Collections.Generic; using UnityEngine; using HTC.UnityPlugin.Vive; public class viveLearn : MonoBehaviour { // Update is called once per frame void Update () { if (ViveInput.GetPress (HandRole.RightHand, ControllerButton.Menu)) { Debug.Log ("menu press"); } if (ViveInput.GetPressUp (HandRole.RightHand, ControllerButton.Menu)) { Debug.Log ("menu press up"); } if (ViveInput.GetPressDown (HandRole.RightHand, ControllerButton.Menu)) { Debug.Log ("menu press down"); } } }
除了上面的方法,还能够经过回掉的方式实现
using System.Collections; using System.Collections.Generic; using UnityEngine; using HTC.UnityPlugin.Vive; public class viveLearn : MonoBehaviour { private void Awake(){ ViveInput.AddPress (HandRole.LeftHand, ControllerButton.Menu, OnMenuPress); } private void OnDestory(){ ViveInput.RemovePress (HandRole.LeftHand, ControllerButton.Menu, OnMenuPress); } private void OnMenuPress(){ Debug.Log ("menu press"); } }
Trigger有模拟值,从0到1,没按的时候是0,所有按下是1。能够经过GetTriggerValue方法得到。
ViveInput.GetTriggerValue (HandRole.LeftHand, false);
当TriggerValue在0.1到0.2时,对应HairTrigger,当TriggerValue超过0.5时对应Trigger,当TriggerValue=1时,对应FullTrigger。
能够简单理解为,轻按=HairTriiger,中度按=Trigger,所有按下=FullTrigger。
pad作那么大,固然除了能够按,还能够返回位置信息。用GetPadAxis方法便可。
ViveInput.GetPadAxis (HandRole.LeftHand, false);触碰位置信息以下图
此外,对应pad,又有接触、按下的两组方法。其中,Axis是坐标位置,Delta是最后一帧移动位置,Vector是移动的向量。
ViveInput.GetPadTouchAxis (HandRole.LeftHand, false); ViveInput.GetPadTouchDelta (HandRole.LeftHand, false); ViveInput.GetPadTouchVector (HandRole.LeftHand, false); ViveInput.GetPadPressAxis (HandRole.LeftHand, false); ViveInput.GetPadPressDelta (HandRole.LeftHand, false); ViveInput.GetPadPressVector (HandRole.LeftHand, false);