using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
public class Xr_input_Record : MonoBehaviour
{
//private UnityEngine.XR.InputDevice device;
// Start is called before the first frame update
public UnityEngine.XR.InputDevice leftHand_device;
public float 振幅=0.5f;
public uint 频道=0;
public float 持续时长=1f;
void Start()
{
// Allinput_device_record();
// get_left_hand();
}
private void OnEnable() {
// Allinput_device_record();
// get_left_hand();
// display_device_featureName();
get_left_hand();
控制器振动功能();
}
public void Allinput_device_record()
{
var inputDevices = new List<UnityEngine.XR.InputDevice>();
UnityEngine.XR.InputDevices.GetDevices(inputDevices);
foreach (var device_a in inputDevices)
{
Debug.Log(string.Format("设备名字 '{0}' 设备角色 '{1}'", device_a.name, device_a.role.ToString()));
}
}
public void get_left_hand()
{
var leftHandedControllers = new List<UnityEngine.XR.InputDevice>();
var desiredCharacteristics = UnityEngine.XR.InputDeviceCharacteristics.HeldInHand | UnityEngine.XR.InputDeviceCharacteristics.Left | UnityEngine.XR.InputDeviceCharacteristics.Controller;
UnityEngine.XR.InputDevices.GetDevicesWithCharacteristics(desiredCharacteristics, leftHandedControllers);
foreach (var device in leftHandedControllers)
{
Debug.Log(string.Format("左手设备名 '{0}' 该设备具有的特征 '{1}'", device.name, device.characteristics.ToString()));
leftHand_device=device;
}
}
public void display_device_featureName()
{
var inputFeatures = new List<UnityEngine.XR.InputFeatureUsage>();
if (leftHand_device.TryGetFeatureUsages(inputFeatures)) //用于获取设备的使用表格
{
foreach (var feature in inputFeatures)
{
if (feature.type == typeof(bool))
{
bool featureValue;
if (leftHand_device.TryGetFeatureValue(feature.As<bool>(), out featureValue))
{
Debug.Log(string.Format("功能名字 {0}'s 功能的值 {1}", feature.name, featureValue.ToString()));
}
}
}
}
}
public void update_inputDynamic_record()
{
bool triggerValue;
//知道按键名字,直接根据名字获得相对应的值
if (leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.gripButton, out triggerValue) && triggerValue)
{
Debug.Log("按下grip button");
}
else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.menuButton, out triggerValue) && triggerValue)
{
Debug.Log("按下menu button");
}
else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primaryButton, out triggerValue) && triggerValue)
{
Debug.Log("按下Primary button");
}
else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primaryTouch, out triggerValue) && triggerValue)
{
Debug.Log("按下Primary touch");
}
else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.secondaryButton, out triggerValue) && triggerValue)
{
Debug.Log("按下second button");
}
else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.secondaryTouch, out triggerValue) && triggerValue)
{
Debug.Log("按下second touch");
}
else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.triggerButton, out triggerValue) && triggerValue)
{
Debug.Log("按下trigger button");
}
else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxisClick, out triggerValue) && triggerValue)
{
Debug.Log("按下primary 2D click button");
}
else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxisTouch, out triggerValue) && triggerValue)
{
Debug.Log("按下primary 2D click touch");
}
/*else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.isTracked, out triggerValue) && triggerValue)
{
Debug.Log("按下is_tracked touch");
}*/
}
public void 控制器振动功能()
{
// List<UnityEngine.XR.InputDevice> devices = new List<UnityEngine.XR.InputDevice>();
print("A1");
UnityEngine.XR.HapticCapabilities capabilities;
if (leftHand_device.TryGetHapticCapabilities(out capabilities))
{
print("A2");
if (capabilities.supportsImpulse)
{
print("A3");
uint channel = 0;
// float amplitude = 5f;
float duration = 5.0f;
leftHand_device.SendHapticImpulse(频道, 振幅, 持续时长);
}
}
}
// Update is called once per frame
void Update()
{
update_inputDynamic_record();
}
}
Quest2 在Unity中获取手柄的输入,以及检测各个按键是否被按下,以及控制手柄振动功能
最新推荐文章于 2024-06-07 11:29:46 发布