HTCVIVE---VRTK3.2.1的手柄按钮方法和功能响应(震动,判断左右手柄,圆盘上下左右)

引用VRTK。

定义一个变量,用来获取判断手柄圆盘按下位置的角度,获取VRTK_ControllerEvents组件

  private VRTK_ControllerEvents Events;
    public float Angle;//按下和抬起圆盘时获取按下位置的角度

    private void Awake()
    {
        Events = transform.GetComponent<VRTK_ControllerEvents>();
    }

注册按键事件:

 /// <summary>
    /// 各种按钮响应事件注册
    /// </summary>
    private void Start()
    {
        Events.ButtonOneTouchStart += Events_ButtonOneTouchStart;

        Events.ButtonTwoTouchStart += Events_ButtonTwoTouchStart;
        Events.ButtonTwoTouchEnd += Events_ButtonTwoTouchEnd;
        Events.ButtonTwoPressed += Events_ButtonTwoPressed;
        Events.ButtonTwoReleased += Events_ButtonTwoReleased;

        Events.TriggerTouchStart += Events_TriggerTouchStart;
        Events.TriggerTouchEnd += Events_TriggerTouchEnd;
        Events.TriggerHairlineStart += Events_TriggerHairlineStart;
        Events.TriggerHairlineEnd += Events_TriggerHairlineEnd;
        Events.TriggerPressed += Event_TriggerPressed;
        Events.TriggerClicked += Events_TriggerClicked;
        Events.TriggerReleased += Events_TriggerReleased;

        Events.TouchpadTouchStart += Events_TouchpadTouchStart;
        Events.TouchpadTouchEnd += Events_TouchpadTouchEnd;
        Events.TouchpadPressed += Events_TouchpadPressed;
        Events.TouchpadReleased += Events_TouchpadReleased;

        Events.GripTouchStart += Events_GripTouchStart;
        Events.GripTouchEnd += Events_GripTouchEnd;
        Events.GripPressed += Events_GripPressed;
        Events.GripClicked += Events_GripClicked;
        Events.GripReleased += Events_GripReleased;

    }

下面对按钮事件做详细说明:

  /// <summary>
    /// 不知道什么按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_ButtonOneTouchStart(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("one");
    }

   /// <summary>
    /// 菜单按钮触摸一次。实测需要按下
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_ButtonTwoTouchStart(object sender, ControllerInteractionEventArgs e)
    {
        if (e.controllerReference == VRTK_ControllerReference.GetControllerReference
            (SDK_BaseController.ControllerHand.Right))//判断是哪个手柄
        {
            Debug.Log("Application---TouchStart");

                                       
                                   //手柄震动                                         
            VRTK_ControllerHaptics.TriggerHapticPulse
                (VRTK_ControllerReference.GetControllerReference(this.gameObject), 3000, 1, 0.01f);
                                         //另一种方法判断是哪个手柄    
        }
    }

    /// <summary>
    /// 菜单按钮触摸结束一次,实测是按下结束
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_ButtonTwoTouchEnd(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Application---TouchEnd");
    }

    /// <summary>
    /// 菜单按钮按下一次
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_ButtonTwoPressed(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Application---Pressed");
    }

    /// <summary>
    /// 菜单按钮抬起一次
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_ButtonTwoReleased(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Application---Released");
    }

    /// <summary>
    /// 扳机键开始触摸一次,实测几乎是按下的瞬间,10°左右
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_TriggerTouchStart(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Trigger---TouchStart");
    }

    /// <summary>
    /// 扳机结束触摸一次
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_TriggerTouchEnd(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Trigger---TouchEnd");
    }

    /// <summary>
    /// 扳机轻微按下一次,大概按下30°左右
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_TriggerHairlineStart(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Trigger---HairlineStart");
    }

    /// <summary>
    /// 扳机轻微按下结束一次
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></pa
    private void Events_TriggerHairlineEnd(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Trigger---HairlineEnd");
    }

    /// <summary>
    /// 扳机按下一次,大概45°
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Event_TriggerPressed(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Trigger---Pressed");
    }

    /// <summary>
    /// 扳机完全按下
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_TriggerClicked(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Trigger---Ckicked");
    }

    /// <summary>
    /// 扳机按下或者完全按下抬起一次。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_TriggerReleased(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Trigger---Released");
    }

    /// <summary>
    /// 圆盘触摸一次
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_TouchpadTouchStart(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Touchpad---ToucheStart");
    }

    /// <summary>
    /// 圆盘触摸结束一次
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_TouchpadTouchEnd(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Touchpad---ToucheEnd");
    }

    /// <summary>
    /// 圆盘按下一次,获取以(0,1)向量为起始边,顺时针旋转的角度大小
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_TouchpadPressed(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Touchpad---Pressed");

        if ((e.touchpadAngle > 0 && e.touchpadAngle <= 45) || (e.touchpadAngle >= 315 && e.touchpadAngle < 360))
        {
            Debug.Log("Up---OnceClick");
        }
        if (e.touchpadAngle > 135 && e.touchpadAngle <= 225)
        {
            Debug.Log("Down---OnceClick");
        }
        if (e.touchpadAngle > 225 && e.touchpadAngle < 315)
        {
            Debug.Log("Left---OnceClick");
        }
        if (e.touchpadAngle > 45 && e.touchpadAngle <= 135)
        {
            Debug.Log("Right---OnceClick");
        }

        Angle = e.touchpadAngle;
    }

    /// <summary>
    /// 圆盘按下松开一次
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_TouchpadReleased(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Touchpad---Released");
        Angle = 0;
    }

    /// <summary>
    /// 侧键触摸一次,测试是需要按下
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_GripTouchStart(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Grip---TouchStart");
    }

    /// <summary>
    /// 侧键触摸结束一次
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_GripTouchEnd(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Grip---TouchEnd");
    }

    /// <summary>
    /// 侧键按下一次
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_GripPressed(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Grip---Pressed");
    }

    /// <summary>
    /// 目前没有发现任何作用
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_GripClicked(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Grip---Clicked");
    }

    /// <summary>
    /// 侧键按下抬起一次
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Events_GripReleased(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Grip---Realeased");
    }

上面就是对应的手柄按钮的各种方法,包含触摸,按下,抬起等,都是执行一次。注释都很清楚了

在Update里面实现长按方法如下:

  /// <summary>
    /// 各种按钮长按监测
    /// </summary>
    private void Update()
    {
        if (Events.buttonTwoPressed)
        {
            Debug.Log("Application---LongPressed");
        }
        if (Events.triggerTouched)
        {
            Debug.Log("Trigger---LongTouched");
        }
        if (Events.triggerHairlinePressed)
        {
            Debug.Log("Trigger---LoneHairlinePressed");
        }
        if (Events.triggerPressed)
        {
            Debug.Log("Trigger---LongPressed");
        }
        if (Events.triggerClicked)
        {
            Debug.Log("Trigger---LongClicked");
        }
        if (Events.touchpadTouched)
        {
            Debug.Log("Touchpad---LongTouch");
        }
        if (Events.touchpadPressed)
        {
            Debug.Log("Touchpad---LongPressed");
        }
        if (Events.gripTouched)
        {
            Debug.Log("Grip---LongTouch");
        }
        if (Events.gripClicked)
        {
            Debug.Log("Grip---LongClicked");
        }


        if ((Angle > 0 && Angle <= 45) || (Angle >= 315 && Angle < 360))
        {
            Debug.Log("Up---LongPress");
        }
        if (Angle > 135 && Angle <= 225)
        {
            Debug.Log("Down---LongPress");
        }
        if (Angle > 225 && Angle < 315)
        {
            Debug.Log("Left---LongPress");
        }
        if (Angle > 45 && Angle <= 135)
        {
            Debug.Log("Right---LongPress");
        }
    }

根据实际情况使用对用的方法

欢迎加群:4364930讨论。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值