1,了解HTCVive手柄按钮对应名称
2,获取手柄输入事件
using UnityEngine;
using System.Collections;
//该脚本挂在[CameraRig] 下的Controller(left or right)即手柄 上
public class WandController : MonoBehaviour {
//1 获取控制器按钮ID
private Valve.VR.EVRButtonId triggerBtn = Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger;//获取trigger Id
private Valve.VR.EVRButtonId touchpadBtn = Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad; //获取touchpad Id
private Valve.VR.EVRButtonId gripBtn = Valve.VR.EVRButtonId.k_EButton_Grip;//获取grip Id
private Valve.VR.EVRButtonId menuBtn = Valve.VR.EVRButtonId.k_EButton_ApplicationMenu;//获取menu Id
//2 定义trackedObj (SteamVR_TrackedObject script 组件挂在[CameraRig] 下的Controller(left or right) 上
private SteamVR_TrackedObject trackedObj;
//3 获取Device类 ,最后通过调用Device类下的方法GetTouch(Press)Down/GetTouch(Press)/GetTouch(Press)Up来获取按键事件
private SteamVR_Controller.Device device { get { return SteamVR_Controller.Input((int)trackedObj.index); } }
// Use this for initialization
void Start () {
trackedObj = GetComponent<SteamVR_TrackedObject>();
}
// Update is called once per frame
void Update () {
if (device == null)
{
Debug.Log("device not initialized");
return;
}
/**
*下面是trigger键按下至松开过程的输入事件
* 按下(只会调用一次)-----> 按住(多次调用)-----> 松开(只会调用一次)
* **/
if (device.GetPressDown(triggerBtn))
{
//Debug.Log("trigger 刚按下");
}
if (device.GetPress(triggerBtn))
{
//Debug.Log("trigger 一直处于按下状态");
}
if (device.GetPressUp(triggerBtn))
{
//Debug.Log("trigger 刚松开");
}
/**
*下面是touchpad的触摸前后输入事件
*/
if (device.GetTouchDown(touchpadBtn))
{
//Debug.Log("手指刚接触触摸板");
}
if (device.GetTouch(touchpadBtn))
{
//Debug.Log("一直接触触摸板");
}
if (device.GetTouchUp(touchpadBtn))
{
//Debug.Log("手指离开触摸板");
}
//手指在触摸板上的位置 为Vector2 类型 x,y 介于-1 至1之间
//Debug.Log("触摸点的位置:"+ device.GetAxis(touchpadBtn));
//扳机键按下的程度 为Vector2 类型 x介于0至1,y 始终为0
//Debug.Log("扳机键的位置:" + device.GetAxis(triggerBtn));
//使用hairTrigger来判断
//device.hairTriggerDelta = 0.5f;//hairTriggerDelta默认是0.1f;
//Debug.Log(device.GetHairTriggerDown());
//手柄震动 参数可以理解为震动强度,默认500(参数超过4000会无效,导致震动不触发)
//device.TriggerHapticPulse(3000);
}
}