Unity中PICO按键事件绑定_unity3d pico 使用inputactionreference触发动作
一、判断手柄按键状态(按下/抬起)
1、定义手柄按键
//定义手柄按键 public InputActionReference left_X_Button; public InputActionReference left_Y_Button; public InputActionReference right_A_Button; public InputActionReference right_B_Button;
在Inspector面板赋值。
1)找到输入管理配置文件,双击打开XRI Default Input Actions文件。
2)自定义一个Action Maps(PICO_XR),定义你需要的按键行为,这里我用到了右手柄的A键和B键,行为类型默认是Button,即响应方式为常规按钮的按下和抬起。
3)选择需要绑定的按键,PICO开发依次选择XR Controller、PICO Controller、PICO Controller(RightHand)、primaryButton(这里我需要的是A键)。按键对应名称我放在最下面了,可以参考一下。
配置完成不要忘记保存!
将创建的按键行为赋值。
2、绑定事件
void Awake()//订阅事件 { //手柄按键按下事件started/performed二选一即可 left_X_Button.action.started += Left_X_ButtonDownStartEvent;//按键按下状态开始 left_X_Button.action.performed += Left_X_ButtonDownEndEvent;//按键按下状态完成 left_X_Button.action.canceled += Left_X_ButtonUpEvent;//按键抬起 left_Y_Button.action.started += Left_Y_ButtonDownStartEvent; left_Y_Button.action.performed += Left_Y_ButtonDownEndEvent; left_Y_Button.action.canceled += Left_Y_ButtonUpEvent; }
3、事件函数
private void Left_X_ButtonDownStartEvent(InputAction.CallbackContext context) { Debug.Log(\"按下左手柄X键\"); //按下行为一进入就触发 } private void Left_X_ButtonDownEndEvent(InputAction.CallbackContext context) { Debug.Log(\"按下左手柄X键\"); //按下行为结束触发 } private void Left_X_ButtonUpEvent(InputAction.CallbackContext context) { Debug.Log(\"抬起左手柄X键\"); //执行按键抬起函数 }
基本按键
//定义手柄按键 //左手柄按键 public InputActionReference left_Grip_Button; public InputActionReference left_Trigger_Button; public InputActionReference left_X_Button; public InputActionReference left_Y_Button; //右手柄按键 public InputActionReference right_Grip_Button; public InputActionReference right_Trigger_Button; public InputActionReference right_A_Button; public InputActionReference right_B_Button; void Awake()//订阅事件 { left_Grip_Button.action.started += Left_Grip_ButtonDownStartEvent; left_Grip_Button.action.performed += Left_Grip_ButtonDownEndEvent; left_Grip_Button.action.canceled += Left_Grip_ButtonUpEvent; left_Trigger_Button.action.started += Left_Trigger_ButtonDownStartEvent; left_Trigger_Button.action.performed += Left_Trigger_ButtonDownEndEvent; left_Trigger_Button.action.canceled += Left_Trigger_ButtonUpEvent; left_X_Button.action.started += Left_X_ButtonDownStartEvent; left_X_Button.action.performed += Left_X_ButtonDownEndEvent; left_X_Button.action.canceled += Left_X_ButtonUpEvent; left_Y_Button.action.started += Left_Y_ButtonDownStartEvent; left_Y_Button.action.performed += Left_Y_ButtonDownEndEvent; left_Y_Button.action.canceled += Left_Y_ButtonUpEvent; right_Grip_Button.action.started += Right_Grip_ButtonDownStartEvent; right_Grip_Button.action.performed += Right_Grip_ButtonDownEndEvent; right_Grip_Button.action.canceled += Right_Grip_ButtonUpEvent; right_Trigger_Button.action.started += Right_Trigger_ButtonDownStartEvent; right_Trigger_Button.action.performed += Right_Trigger_ButtonDownEndEvent; right_Trigger_Button.action.canceled += Right_Trigger_ButtonUpEvent; right_A_Button.action.started += Right_A_ButtonDownStartEvent; right_A_Button.action.performed += Right_A_ButtonDownEndEvent; right_A_Button.action.canceled += Right_A_ButtonUpEvent; right_B_Button.action.started += Right_B_ButtonDownStartEvent; right_B_Button.action.performed += Right_B_ButtonDownEndEvent; right_B_Button.action.canceled += Right_B_ButtonUpEvent; } private void Left_Grip_ButtonDownStartEvent(InputAction.CallbackContext context) { Debug.Log(\"左手柄握把键开始按下\"); } private void Left_Grip_ButtonDownEndEvent(InputAction.CallbackContext context) { Debug.Log(\"左手柄握把键结束按下\"); } private void Left_Grip_ButtonUpEvent(InputAction.CallbackContext context) { Debug.Log(\"左手柄握把键抬起\"); } private void Left_Trigger_ButtonDownStartEvent(InputAction.CallbackContext context) { Debug.Log(\"左手柄扳机键开始按下\"); } private void Left_Trigger_ButtonDownEndEvent(InputAction.CallbackContext context) { Debug.Log(\"左手柄扳机键结束按下\"); } private void Left_Trigger_ButtonUpEvent(InputAction.CallbackContext context) { Debug.Log(\"左手柄扳机键抬起\"); } private void Left_X_ButtonDownStartEvent(InputAction.CallbackContext context) { Debug.Log(\"左手柄X键开始按下\"); } private void Left_X_ButtonDownEndEvent(InputAction.CallbackContext context) { Debug.Log(\"左手柄X键结束按下\"); } private void Left_X_ButtonUpEvent(InputAction.CallbackContext context) { Debug.Log(\"左手柄X键抬起\"); } private void Left_Y_ButtonDownStartEvent(InputAction.CallbackContext context) { Debug.Log(\"左手柄Y键开始按下\"); } private void Left_Y_ButtonDownEndEvent(InputAction.CallbackContext context) { Debug.Log(\"左手柄Y键结束按下\"); } private void Left_Y_ButtonUpEvent(InputAction.CallbackContext context) { Debug.Log(\"左手柄Y键抬起\"); } private void Right_Grip_ButtonDownStartEvent(InputAction.CallbackContext context) { Debug.Log(\"右手柄握把键开始按下\"); } private void Right_Grip_ButtonDownEndEvent(InputAction.CallbackContext context) { Debug.Log(\"右手柄握把键结束按下\"); } private void Right_Grip_ButtonUpEvent(InputAction.CallbackContext context) { Debug.Log(\"右手柄握把键抬起\"); } private void Right_Trigger_ButtonDownStartEvent(InputAction.CallbackContext context) { Debug.Log(\"右手柄扳机键开始按下\"); } private void Right_Trigger_ButtonDownEndEvent(InputAction.CallbackContext context) { Debug.Log(\"右手柄扳机键结束按下\"); } private void Right_Trigger_ButtonUpEvent(InputAction.CallbackContext context) { Debug.Log(\"右手柄扳机键抬起\"); } private void Right_A_ButtonDownStartEvent(InputAction.CallbackContext context) { Debug.Log(\"右手柄A键开始按下\"); } private void Right_A_ButtonDownEndEvent(InputAction.CallbackContext context) { Debug.Log(\"右手柄A键结束按下\"); } private void Right_A_ButtonUpEvent(InputAction.CallbackContext context) { Debug.Log(\"右手柄A键抬起\"); } private void Right_B_ButtonDownStartEvent(InputAction.CallbackContext context) { Debug.Log(\"右手柄B键开始按下\"); } private void Right_B_ButtonDownEndEvent(InputAction.CallbackContext context) { Debug.Log(\"右手柄B键结束按下\"); } private void Right_B_ButtonUpEvent(InputAction.CallbackContext context) { Debug.Log(\"右手柄B键抬起\"); }}