Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
namespace xrdnk.DebugScripts
{
public class DebugAudioHapticsEvents : MonoBehaviour
{
[SerializeField] private XRBaseControllerInteractor _controllerInteractor;
[Header("Audios")]
[SerializeField] private AudioClip _hoverEnteredSE = null;
[SerializeField] private AudioClip _hoverExitedSE = null;
[SerializeField] private AudioClip _selectEnteredSE = null;
[SerializeField] private AudioClip _selectExitedSE = null;
[Header("Haptics")]
[SerializeField] private float _hapticDuration = DEFAULT_DURATION;
[SerializeField, Range(0, 1)] private float _hapticIntensity = DEFAULT_INTENSITY;
private static readonly float DEFAULT_DURATION = 0.3f;
private static readonly float DEFAULT_INTENSITY = 0.2f;
private bool PlayAudioClipOnHoverEntered => _hoverEnteredSE != null;
private bool PlayAudioClipOnHoverExited => _hoverExitedSE != null;
private bool PlayAudioClipOnSelectEntered => _selectEnteredSE != null;
private bool PlayAudioClipOnSelectExited => _selectExitedSE != null;
private bool PlayHaptics => _hapticDuration > 0.0f;
private void Awake()
{
// 音を有効にするかどうか (デフォルトは false).音が登録されていたらtrue.
_controllerInteractor.playAudioClipOnHoverEntered = PlayAudioClipOnHoverEntered;
_controllerInteractor.playAudioClipOnHoverExited = PlayAudioClipOnHoverExited;
_controllerInteractor.playAudioClipOnSelectEntered = PlayAudioClipOnSelectEntered;
_controllerInteractor.playAudioClipOnSelectExited = PlayAudioClipOnSelectExited;
// 音の登録
_controllerInteractor.audioClipForOnHoverEntered = _hoverEnteredSE;
_controllerInteractor.audioClipForOnHoverExited = _hoverExitedSE;
_controllerInteractor.audioClipForOnSelectEntered = _selectEnteredSE;
_controllerInteractor.audioClipForOnSelectExited = _selectExitedSE;
// 振動を有効にするかどうか (デフォルトは false).間隔が0より大きいならtrue.(今回各モードごとの設定が面倒なので,集約化)
_controllerInteractor.playHapticsOnHoverEntered = PlayHaptics;
_controllerInteractor.playHapticsOnHoverExited = PlayHaptics;
_controllerInteractor.playHapticsOnSelectEntered = PlayHaptics;
_controllerInteractor.playHapticsOnHoverExited = PlayHaptics;
// 振動間隔の設定
_controllerInteractor.hapticHoverEnterDuration = _hapticDuration;
_controllerInteractor.hapticHoverExitDuration = _hapticDuration;
_controllerInteractor.hapticSelectEnterDuration = _hapticDuration;
_controllerInteractor.hapticSelectExitDuration = _hapticDuration;
// 振動強度の設定
_controllerInteractor.hapticHoverEnterIntensity = _hapticIntensity;
_controllerInteractor.hapticHoverExitIntensity = _hapticIntensity;
_controllerInteractor.hapticSelectEnterIntensity = _hapticIntensity;
_controllerInteractor.hapticSelectExitIntensity = _hapticIntensity;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment