Files
FutureMen/Assets/Scripts/MRInput.cs

83 lines
2.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.XR.PXR;
using UnityEngine;
using UnityEngine.XR;
using static Unity.XR.PXR.PXR_Input;
public class MRInput : MonoBehaviour
{
public static MRInput Ins { get; private set; }
private InputDevice _leftHandDevice;
private InputDevice _rightHandDevice;
#if UNITY_EDITOR
//[UnityEngine.DisplayOnly]
#endif
public bool pressRightTrigger = false;
private bool _lastPressRightTrigger = false;
private List<System.Action> _clickTriggerhandlers = new List<System.Action>();
void Start()
{
Ins = this;
#if !UNITY_EDITOR && UNITY_ANDROID && (PICO || VIVE)
_leftHandDevice = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
_rightHandDevice = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
#endif
}
/// <summary>
/// 震动右手控制器
/// </summary>
public void VibrateRightController(float amplitude, int duration, int frequency = 150)
{
#if !UNITY_EDITOR && UNITY_ANDROID && (PICO || VIVE)
PXR_Input.SendHapticImpulse(VibrateType.RightController, amplitude, duration, frequency);
//TrueGearEffectManager.Ins.OnHit(true,4,true);
#endif
}
/// <summary>
/// 震动左手控制器
/// </summary>
public void VibrateLeftController(float amplitude, int duration, int frequency = 150)
{
#if !UNITY_EDITOR && UNITY_ANDROID && (PICO || VIVE)
PXR_Input.SendHapticImpulse(VibrateType.LeftController, amplitude, duration, frequency);
#endif
}
void Update()
{
#if !UNITY_EDITOR && UNITY_ANDROID && (PICO || VIVE)
bool isTrigger = false;
bool isGrip = false;
_rightHandDevice.TryGetFeatureValue(CommonUsages.triggerButton, out isTrigger);
_rightHandDevice.TryGetFeatureValue(CommonUsages.gripButton, out isGrip);
pressRightTrigger=isTrigger||isGrip;
if (!_lastPressRightTrigger && pressRightTrigger)
{
for (int i = 0; i < _clickTriggerhandlers.Count; i++)
{
_clickTriggerhandlers[i].Invoke();
}
}
_lastPressRightTrigger = isTrigger||isGrip;
#elif UNITY_EDITOR
pressRightTrigger = Input.GetKey(KeyCode.Alpha2) ? true : false;
if (Input.GetKeyDown(KeyCode.Alpha2))
{
for (int i = 0; i < _clickTriggerhandlers.Count; i++)
{
_clickTriggerhandlers[i].Invoke();
}
}
#endif
}
}