SCSlider3DSounds.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // Licensed under the MIT License. See LICENSE in the project root for license information.
  4. //
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using UnityEngine.EventSystems;
  10. namespace SC.XR.Unity
  11. {
  12. /// <summary>
  13. /// Component that plays sounds to communicate the state of a pinch slider
  14. /// </summary>
  15. [RequireComponent(typeof(SCSlider3D))]
  16. public class SCSlider3DSounds : MonoBehaviour
  17. {
  18. [Header("Audio Clips")]
  19. [SerializeField]
  20. [Tooltip("Sound to play when interaction with slider starts")]
  21. private AudioClip interactionStartSound = null;
  22. [SerializeField]
  23. [Tooltip("Sound to play when interaction with slider ends")]
  24. private AudioClip interactionEndSound = null;
  25. [Header("Tick Notch Sounds")]
  26. [SerializeField]
  27. [Tooltip("Whether to play 'tick tick' sounds as the slider passes notches")]
  28. private bool playTickSounds = true;
  29. [SerializeField]
  30. [Tooltip("Sound to play when slider passes a notch")]
  31. private AudioClip passNotchSound = null;
  32. [Range(0, 1)]
  33. [SerializeField]
  34. private float tickEvery = 0.1f;
  35. [SerializeField]
  36. private float startPitch = 0.75f;
  37. [SerializeField]
  38. private float endPitch = 1.25f;
  39. [SerializeField]
  40. private float minSecondsBetweenTicks = 0.01f;
  41. #region Private members
  42. private SCSlider3D slider;
  43. // Play sound when passing through slider notches
  44. private float accumulatedDeltaSliderValue = 0;
  45. private float lastSoundPlayTime;
  46. private AudioSource grabReleaseAudioSource = null;
  47. private AudioSource passNotchAudioSource = null;
  48. #endregion
  49. private float oldValue = 0f;
  50. private void Start()
  51. {
  52. if (grabReleaseAudioSource == null)
  53. {
  54. grabReleaseAudioSource = gameObject.AddComponent<AudioSource>();
  55. }
  56. if (passNotchAudioSource == null)
  57. {
  58. passNotchAudioSource = gameObject.AddComponent<AudioSource>();
  59. }
  60. slider = GetComponent<SCSlider3D>();
  61. slider.onPointerDown.AddListener(OnInteractionStarted);
  62. slider.onPointerUp.AddListener(OnInteractionEnded);
  63. slider.onValueChanged.AddListener(OnValueChange);
  64. }
  65. private void OnValueChange(float value)
  66. {
  67. if (playTickSounds && passNotchAudioSource != null && passNotchSound != null)
  68. {
  69. float delta = value - oldValue;
  70. accumulatedDeltaSliderValue += Mathf.Abs(delta);
  71. oldValue = value;
  72. var now = Time.timeSinceLevelLoad;
  73. if (accumulatedDeltaSliderValue > tickEvery && now - lastSoundPlayTime > minSecondsBetweenTicks)
  74. {
  75. passNotchAudioSource.pitch = Mathf.Lerp(startPitch, endPitch, value - oldValue);
  76. passNotchAudioSource.PlayOneShot(passNotchSound);
  77. accumulatedDeltaSliderValue = 0;
  78. lastSoundPlayTime = now;
  79. }
  80. }
  81. }
  82. private void OnInteractionEnded()
  83. {
  84. if (interactionEndSound != null && grabReleaseAudioSource != null)
  85. {
  86. grabReleaseAudioSource.PlayOneShot(interactionEndSound);
  87. }
  88. }
  89. private void OnInteractionStarted()
  90. {
  91. if (interactionStartSound != null && grabReleaseAudioSource != null)
  92. {
  93. grabReleaseAudioSource.PlayOneShot(interactionStartSound);
  94. }
  95. }
  96. }
  97. }