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脚本