1. Action mapping, the specific mapping method can refer to the link:
https://blog.csdn.net/weixin_38484443/article/details/124718029?spm=1001.2014.3001.5501
2. Preliminary preparation
Create a new scene 01-ActionListener, delete the default camera, and drag the ViveCameraRig prefab into the scene
3. Write the code
to create a new Demo1_ActionListener script and mount it on the ViveCameraRig. There are two ways to obtain actions/keys. The first is through SteamVRInput to obtain, the advantage is that after the project is packaged, you can change the action button settings at will. The second is to obtain a specific button through VIveInputUtility's own API, and directly upload the code:
using HTC.UnityPlugin.Vive;
using UnityEngine;
using Valve.VR;
public class Demo1_ActionListener : MonoBehaviour
{
private void Start()
{
Demo_GetAction();
}
private void Update()
{
Demo_GetKey();
}
/// <summary>
/// 获取动作
/// </summary>
private void Demo_GetAction()
{
SteamVR_Actions.htc_viu_viu_press_33.onStateDown += Htc_viu_viu_press_33_onStateDown;
SteamVR_Actions.htc_viu_viu_press_33.AddOnStateDownListener(Htc_viu_viu_press_33_onStateDown, SteamVR_Input_Sources.RightHand);
}
/// <summary>
/// 获取按键
/// </summary>
private void Demo_GetKey()
{
if(ViveInput.GetPressDown(HandRole.RightHand, ControllerButton.Grip))
{
Debug.Log("按下了右手柄握持键");
}
}
private void Htc_viu_viu_press_33_onStateDown(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
{
Debug.Log($"{
fromSource} onstateDown {
fromAction.GetShortName()}");
}
}
We can enter the SteamVR Input interface to view the action mapping under htc_viu, such as: viu_press_33, the action is mapped to the trigger button, and the above is the way to obtain the action/button.