使用新版本的Unity后,VR开发工具换了一遍,插件也转换为了XRTookit,一直想自定义手柄的震动,查了不少Unity的XR api,并参考 Valem Tutorials 的代码。如下:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.XR.Interaction.Toolkit;
[System.Serializable]
public class Haptic
{[Range(0, 1)]public float instensity;public float duration;public void TriggerHaptic(BaseInteractionEventArgs eventArgs){if (eventArgs.interactableObject is XRBaseControllerInteractor controllerInteractor){TriggerHaptic(controllerInteractor.xrController);}}public void TriggerHaptic(XRBaseController controller){if (instensity > 0)controller.SendHapticImpulse(instensity, duration);}
}
///
/// 挂载在物体上的,物体上还需要XRGrabInteractable.cs
///
public class HapticInteractable : MonoBehaviour
{public Haptic hapticOnActivated;public Haptic hapticHoverEntered;public Haptic hapticHoverExited;public Haptic hapticSelectEntered;public Haptic hapticSelectExited;void Start(){XRBaseInteractable interactable = GetComponent();interactable.activated.AddListener(hapticOnActivated.TriggerHaptic);interactable.hoverEntered.AddListener(hapticHoverEntered.TriggerHaptic);interactable.hoverExited.AddListener(hapticHoverExited.TriggerHaptic);interactable.selectEntered.AddListener(hapticSelectEntered.TriggerHaptic);interactable.selectExited.AddListener(hapticSelectExited.TriggerHaptic);}
}
上一篇:C动态数组