Unity开发HTC vive 一、手柄按钮

来源:互联网 发布:淘宝买花苗 编辑:程序博客网 时间:2024/05/16 15:36


认识下vive的手柄

请看下图


手柄两个一对,分左右,开发的时候也是分左右的。每个上面有一个pad和4个按钮。简单说明下:

  • 开关、系统菜单按钮:只有这个按钮不可以编程(默认),用来打开手柄,其实没用关的功能。在游戏中按下该按钮是调出系统默认的菜单,用来关闭,切换游戏用的。
  • menu按钮:默认用来打开游戏菜单。
  • grip按钮:用的最少的按钮,每个手柄上虽然有两个,但是是相同的。
  • trigger按钮:扳机按钮,用的最多,可以有力度。
  • pad:触摸屏+鼠标的功能,可触摸,可点击。

SDK下载

需要两个插件:

SteamVR Plugin

Vive Input Utility

商城地址如下,都是免费的,下载后导入

https://www.assetstore.Unity3D.com/cn/#!/content/32647

https://www.assetstore.unity3d.com/cn/#!/content/64219


程序开发之综述

首先,引用HTC.UnityPlugin.Vive
[csharp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. using HTC.UnityPlugin.Vive;  
using HTC.UnityPlugin.Vive;


每个按钮包括pad都有GetPress、GetPressDown、GetPressUp三种方法,用HandRole枚举来确定左右手柄,用ControllerButton枚举来确定是哪个按钮。

对于按钮,GetPressDown是按下时触发,GetPressUp是放开时触发,以上两个是个事件,GetPress是按住时一直返回ture,算是一个状态。

对于pad,有两种:

当ControllerButton.Pad时,和按钮相同。

当ControllerButton.PadTouch时,GetPressDown是接触时触发,GetPressUp是离开时触发,GetPress是接触时一直返回的状态。

[csharp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. using HTC.UnityPlugin.Vive;  
  5.   
  6. public class viveLearn : MonoBehaviour {  
  7.       
  8.     // Update is called once per frame  
  9.     void Update () {  
  10.         if (ViveInput.GetPress (HandRole.RightHand, ControllerButton.Menu)) {  
  11.             Debug.Log (”menu press”);  
  12.         }  
  13.   
  14.         if (ViveInput.GetPressUp (HandRole.RightHand, ControllerButton.Menu)) {  
  15.             Debug.Log (”menu press up”);  
  16.         }  
  17.   
  18.         if (ViveInput.GetPressDown (HandRole.RightHand, ControllerButton.Menu)) {  
  19.             Debug.Log (”menu press down”);  
  20.         }  
  21.     }  
  22. }  
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");        }    }}



除了上面的方法,还可以通过回掉的方式实现

[csharp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. using HTC.UnityPlugin.Vive;  
  5.   
  6. public class viveLearn : MonoBehaviour {  
  7.   
  8.     private void Awake(){  
  9.         ViveInput.AddPress (HandRole.LeftHand, ControllerButton.Menu, OnMenuPress);  
  10.     }  
  11.   
  12.     private void OnDestory(){  
  13.         ViveInput.RemovePress (HandRole.LeftHand, ControllerButton.Menu, OnMenuPress);  
  14.     }  
  15.   
  16.     private void OnMenuPress(){  
  17.         Debug.Log (”menu press”);  
  18.     }  
  19. }  
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按钮开发

Trigger有模拟值,从0到1,没按的时候是0,全部按下是1。可以通过GetTriggerValue方法获得。

[csharp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. ViveInput.GetTriggerValue (HandRole.LeftHand, false);  
ViveInput.GetTriggerValue (HandRole.LeftHand, false);


Trigger按钮,对应有三种,Trigger,HairTrigger,FullTrigger。

当TriggerValue在0.1到0.2时,对应HairTrigger,当TriggerValue超过0.5时对应Trigger,当TriggerValue=1时,对应FullTrigger。

可以简单理解为,轻按=HairTriiger,中度按=Trigger,全部按下=FullTrigger。


Pad开发

pad做那么大,当然除了可以按,还可以返回位置信息。用GetPadAxis方法即可。

[csharp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. ViveInput.GetPadAxis (HandRole.LeftHand, false);  
ViveInput.GetPadAxis (HandRole.LeftHand, false);
触碰位置信息如下图




此外,对应pad,又有接触、按下的两组方法。其中,Axis是坐标位置,Delta是最后一帧移动位置,Vector是移动的向量。

[csharp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. ViveInput.GetPadTouchAxis (HandRole.LeftHand, false);  
  2. ViveInput.GetPadTouchDelta (HandRole.LeftHand, false);  
  3. ViveInput.GetPadTouchVector (HandRole.LeftHand, false);  
  4.   
  5. ViveInput.GetPadPressAxis (HandRole.LeftHand, false);  
  6. ViveInput.GetPadPressDelta (HandRole.LeftHand, false);  
  7. ViveInput.GetPadPressVector (HandRole.LeftHand, false);  
       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);


1 0