HTC Vive Cosmos開發——手柄按鈕事件

ToDoNothing發表於2020-10-07

1.環境配置

htc vive cosmos的環境配置比較簡單,在HTC VIVE官網下載VIVEPORT客戶端即可:網址:viveport

2.手柄按鍵事件(unity+VRTK)

(1)將VRTK和Steam VR匯入,steam vr版本不能太新,按VRTK官網推薦的即可,我用的steam vr版本是1.2.3,unity版本為2019.4.8f1。官網傳送:VRTK
(2)其次就是,在Player Setting裡面,需要把OpenVR放在最上面,把None去掉。
在這裡插入圖片描述

(3)手柄的按鍵對應可以在VRTK的事件機制中試出來,方法也很簡單,在手柄的對映中新增一個指令碼。
在這裡插入圖片描述
(4)程式碼也很簡單,如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;

public class TriggerButtonTest : MonoBehaviour
{
    private VRTK_ControllerEvents controllerEvents;
    void Start()
    {
        controllerEvents = GetComponent<VRTK_ControllerEvents>();
        controllerEvents.TriggerPressed += DoTriggerPressed;
        //ButtonOnePressed對應 Cosmos手柄為X鍵
        controllerEvents.ButtonOnePressed += ButtonOnePress;
        //ButtonTwoPressed對應 Cosmos手柄為Y鍵
        controllerEvents.ButtonTwoPressed += ButtonTwoPress;
        //GripPressed對應 Cosmos手柄為Grip鍵
        controllerEvents.GripPressed += GripPress;
        //TouchpadPressed對應 Cosmos手柄為Grip鍵
        controllerEvents.TouchpadPressed += TouchpadPress;


    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void DoTriggerPressed(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("Trigger Press");
    }
    private void ButtonOnePress(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("ButtonOnePress ");
    }
    private void ButtonTwoPress(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("ButtonTwoPress");
    }
    private void StartMenuPress(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("StartMenuPress ");
    }

    private void GripPress(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("GripPress ");
    }

    private void TouchpadPress(object sender, ControllerInteractionEventArgs e)
    {
        Debug.Log("TouchpadPress ");
    }
}

相關文章