1、首先創建VRScripts空物體,用來存放腳本,在其下創建Right空物體並添加VRTK_ControllerEvents腳本
2、Right作為右手手柄,拖拽到[VRTK_SDKManager]的RightController上
3丶在Right上新建腳本RightController,通過得到VRTK_ControllerEvents組件注冊方法,就可以監聽到按下手柄哪些鍵了
using System.Collections; using System.Collections.Generic; using UnityEngine; using VRTK;// 引用VRTK命名空間 public class RightController: MonoBehaviour { private VRTK_ControllerEvents controllerEvents; private void Awake() { controllerEvents = GetComponent<VRTK_ControllerEvents>(); // 輸入+=后按Tab鍵后自動補全方法 controllerEvents.TouchpadPressed += ControllerEvents_TouchpadPressed; controllerEvents.TouchpadReleased += ControllerEvents_TouchpadReleased; controllerEvents.GripPressed += ControllerEvents_GripPressed; } private void ControllerEvents_GripPressed(object sender, ControllerInteractionEventArgs e) { Debug.Log("抓取鍵按下"); } private void ControllerEvents_TouchpadReleased(object sender, ControllerInteractionEventArgs e) { Debug.Log("圓盤鍵按釋放"); } private void ControllerEvents_TouchpadPressed(object sender, ControllerInteractionEventArgs e) { Debug.Log("圓盤鍵按下"); } }
還有許多方法這里不逐條列出,大家自行查看VRTK_ControllerEvents腳本
4丶注意:只要涉及到操作手柄,就必須要添加VRTK_ControllerEvents腳本