HTC VIVE 手柄 接入(手柄上的按键都有说明)

一、资源导入

从asset store 下载 SteamVR Plugin。然后导入Unity。


将上图中的prefab拖入场景,然后在steam上装好steamVR,就可以运行了。

二、手柄按键使用说明

触控板的坐标如下图


直接上代码,哪里不懂看注解就好。controller在刚才拖进场景的prefab里,如下图。

[csharp]  view plain  copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3. //检测手柄功能的脚本 这个脚本挂到手柄上(controller(right)和controller(left))上      
  4. public class HTCStick : MonoBehaviour {  
  5.     //手柄      
  6.     SteamVR_TrackedObject trackdeObjec;  
  7.     void Awake ( ) {  
  8.         //获取手柄上的这个组件      
  9.         trackdeObjec = GetComponent<SteamVR_TrackedObject> ();  
  10.     }  
  11.     // Use this for initialization      
  12.     void Start ( ) {  
  13.     }  
  14.     void FixedUpdate ( ) {   //获取手柄输入      
  15.         var device = SteamVR_Controller.Input ((int)trackdeObjec.index);  
  16.         //以下是api中复制出来的按键列表      
  17.         /*       public class ButtonMask   
  18.            {   
  19.                public const ulong System = (1ul << (int)EVRButtonId.k_EButton_System); // reserved   
  20.                public const ulong ApplicationMenu = (1ul << (int)EVRButtonId.k_EButton_ApplicationMenu);   
  21.                public const ulong Grip = (1ul << (int)EVRButtonId.k_EButton_Grip);   
  22.                public const ulong Axis0 = (1ul << (int)EVRButtonId.k_EButton_Axis0);   
  23.                public const ulong Axis1 = (1ul << (int)EVRButtonId.k_EButton_Axis1);   
  24.                public const ulong Axis2 = (1ul << (int)EVRButtonId.k_EButton_Axis2);   
  25.                public const ulong Axis3 = (1ul << (int)EVRButtonId.k_EButton_Axis3);   
  26.                public const ulong Axis4 = (1ul << (int)EVRButtonId.k_EButton_Axis4);   
  27.                public const ulong Touchpad = (1ul << (int)EVRButtonId.k_EButton_SteamVR_Touchpad);   
  28.                public const ulong Trigger = (1ul << (int)EVRButtonId.k_EButton_SteamVR_Trigger);   
  29.            }   
  30.            */  
  31.   
  32.         //每种按键都有GetTouch、GetTouchDown、GetTouchUp、GetPressDown、GetPress、GetPressUp  
  33.         //这里只有Trigger扳机键写了6种,其他的不再重复。  
  34.         //Trigger的Touch触发条件是扳机键没有按到底,此时不会触发press。触发press时必定触发touch。      
  35.         if (device.GetTouch (SteamVR_Controller.ButtonMask.Trigger)) {  
  36.             Debug.Log ("轻按了扳机键");  
  37.             //右手震动      
  38.             //拉弓类似操作应该就是按住trigger(扳机)gettouch时持续调用震动方法模拟弓弦绷紧的感觉。      
  39.             var deviceIndex2 = SteamVR_Controller.GetDeviceIndex (SteamVR_Controller.DeviceRelation.Rightmost);  
  40.             device.TriggerHapticPulse (1200);  
  41.         }  
  42.   
  43.         if (device.GetTouchDown (SteamVR_Controller.ButtonMask.Trigger)) {  
  44.             Debug.Log ("轻按了扳机键");  
  45.         }  
  46.   
  47.         if (device.GetTouchUp (SteamVR_Controller.ButtonMask.Trigger)) {  
  48.             Debug.Log ("松开了扳机键");  
  49.   
  50.             //左手震动      
  51.             var deviceIndex = SteamVR_Controller.GetDeviceIndex (SteamVR_Controller.DeviceRelation.Leftmost);  
  52.             SteamVR_Controller.Input (deviceIndex).TriggerHapticPulse (3000);  
  53.   
  54.             //右手震动      
  55.             var deviceIndex1 = SteamVR_Controller.GetDeviceIndex (SteamVR_Controller.DeviceRelation.Rightmost);  
  56.             SteamVR_Controller.Input (deviceIndex1).TriggerHapticPulse (3000);  
  57.         }  
  58.   
  59.         if (device.GetPressDown (SteamVR_Controller.ButtonMask.Trigger)) {  
  60.             Debug.Log ("用press按下了trigger扳机键");  
  61.         }  
  62.         if (device.GetPress (SteamVR_Controller.ButtonMask.Trigger)) {  
  63.             Debug.Log ("用press按了trigger扳机键");  
  64.         }  
  65.         if (device.GetPressUp (SteamVR_Controller.ButtonMask.Trigger)) {  
  66.             Debug.Log ("用press松开了trigger扳机键");  
  67.         }  
  68.   
  69.         //system键 圆盘下面那个键       
  70.         // reserved 为Steam系统保留,用来调出Steam系统菜单 因此自己加的功能没用  下面的打印不会出现     
  71.         if (device.GetTouchDown (SteamVR_Controller.ButtonMask.System)) {  
  72.             Debug.Log ("按下了system系统按钮");  
  73.         }  
  74.         if (device.GetPressDown (SteamVR_Controller.ButtonMask.System)) {  
  75.             Debug.Log ("用press按下了系统按钮");  
  76.         }  
  77.   
  78.         //ApplicationMenu键 带菜单标志的那个按键(在方向圆盘上面)      
  79.         //ApplicationMenu键 的Touch和Press没有区别,触发都要按下去  
  80.         if (device.GetTouchDown (SteamVR_Controller.ButtonMask.ApplicationMenu)) {  
  81.             Debug.Log ("按下了 ApplicationMenu菜单键");  
  82.         }  
  83.         if (device.GetPressDown (SteamVR_Controller.ButtonMask.ApplicationMenu)) {  
  84.             Debug.Log ("用press按下了ApplicationMenu菜单键");  
  85.         }  
  86.   
  87.         //Grip键 手柄两侧的按键 每个手柄左右各一且功能相同,同一手柄两个键是一个键。     
  88.         //Grip键 的Touch和Press没有区别,触发都要按下去  
  89.         if (device.GetTouchDown (SteamVR_Controller.ButtonMask.Grip)) {  
  90.             Debug.Log ("按下了 Grip");  
  91.         }  
  92.         if (device.GetPressDown (SteamVR_Controller.ButtonMask.Grip)) {  
  93.             Debug.Log ("用press按下了 Grip");  
  94.         }  
  95.   
  96.         //Axis0键和Touchpad是等价的 与圆盘有关 详情看下面的TouchPad这里不再赘述  
  97.         //触摸触发      
  98.         if (device.GetTouchDown (SteamVR_Controller.ButtonMask.Axis0)) {  
  99.             Debug.Log ("按下了 Axis0");  
  100.         }  
  101.         //按动触发      
  102.         if (device.GetPressDown (SteamVR_Controller.ButtonMask.Axis0)) {  
  103.             Debug.Log ("用press按下了Axis0");  
  104.         }  
  105.   
  106.         //Axis1键  等价于Trigger键详情看上面的Trigger按钮 这里不再赘述  
  107.         //触摸触发      
  108.         if (device.GetTouchDown (SteamVR_Controller.ButtonMask.Axis1)) {  
  109.             Debug.Log ("按下了Axis1");  
  110.         }  
  111.         //按动触发       
  112.         if (device.GetPressDown (SteamVR_Controller.ButtonMask.Axis1)) {  
  113.             Debug.Log ("用press按下了Axis1");  
  114.         }  
  115.   
  116.         //Axis2键 目前未发现按键位置      
  117.         //触摸触发      
  118.         if (device.GetTouchDown (SteamVR_Controller.ButtonMask.Axis2)) {  
  119.             Debug.Log ("按下了 Axis2");  
  120.         }  
  121.         //按动触发      
  122.         if (device.GetPressDown (SteamVR_Controller.ButtonMask.Axis2)) {  
  123.             Debug.Log ("用press按下了Axis2");  
  124.         }  
  125.   
  126.         //Axis3键  目前未发现按键位置      
  127.         //触摸触发      
  128.         if (device.GetTouchDown (SteamVR_Controller.ButtonMask.Axis3)) {  
  129.             Debug.Log ("按下了Axis3");  
  130.         }  
  131.         //按动触发      
  132.         if (device.GetPressDown (SteamVR_Controller.ButtonMask.Axis3)) {  
  133.             Debug.Log ("用press按下了Axis3");  
  134.         }  
  135.   
  136.         //Axis4键  目前未发现按键位置      
  137.         //触摸触发      
  138.         if (device.GetTouchDown (SteamVR_Controller.ButtonMask.Axis4)) {  
  139.             Debug.Log ("按下了Axis4");  
  140.         }  
  141.         //按动触发      
  142.         if (device.GetPressDown (SteamVR_Controller.ButtonMask.Axis4)) {  
  143.             Debug.Log ("用press按下了Axis4");  
  144.         }  
  145.   
  146.         //Touchpad键 圆盘交互      
  147.         //触摸触发      
  148.         if (device.GetTouchDown (SteamVR_Controller.ButtonMask.Touchpad)) {  
  149.             Debug.Log ("按下了 Touchpad");  
  150.   
  151.             //方法返回一个坐标 接触圆盘位置      
  152.             Vector2 cc = device.GetAxis ();  
  153.             Debug.Log (cc);  
  154.             // 例子:圆盘分成上下左右      
  155.             float angle = VectorAngle (new Vector2 (1, 0), cc);  
  156.             Debug.Log (angle);  
  157.             //下      
  158.             if (angle > 45 && angle < 135) {  
  159.                 Debug.Log ("下");  
  160.             }  
  161.             //上      
  162.             if (angle < -45 && angle > -135) {  
  163.                 Debug.Log ("上");  
  164.             }  
  165.             //左      
  166.             if ((angle < 180 && angle > 135) || (angle < -135 && angle > -180)) {  
  167.                 Debug.Log ("左");  
  168.             }  
  169.             //右      
  170.             if ((angle > 0 && angle < 45) || (angle > -45 && angle < 0)) {  
  171.                 Debug.Log ("右");  
  172.             }  
  173.         }  
  174.         //按动触发      
  175.         if (device.GetPressDown (SteamVR_Controller.ButtonMask.Touchpad)) {  
  176.             Debug.Log ("用press按下了Touchpad");  
  177.             Vector2 cc = device.GetAxis ();  
  178.             Debug.Log (cc);  
  179.         }  
  180.     }  
  181.     // Update is called once per frame      
  182.     void Update ( ) {  
  183.   
  184.     }  
  185.     //方向圆盘最好配合这个使用 圆盘的.GetAxis()会检测返回一个二位向量,可用角度划分圆盘按键数量      
  186.     //这个函数输入两个二维向量会返回一个夹角 180 到 -180      
  187.     float VectorAngle (Vector2 from, Vector2 to) {  
  188.         float angle;  
  189.         Vector3 cross = Vector3.Cross (from, to);  
  190.         angle = Vector2.Angle (from, to);  
  191.         return cross.z > 0 ? -angle : angle;  
  192.     }  
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值