AudioPlayer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class AudioPlayer : MonoBehaviour
  5. {
  6. // =========================================================================
  7. // Background Themes
  8. public AudioSource BackgroundThemeBotanicalRoom;
  9. public AudioSource BackgroundThemeMachineRoom;
  10. public AudioSource BackgroundThemeCryoRoom;
  11. public AudioSource BackgroundThemeKitchenRoom;
  12. public AudioSource BackgroundThemeControlRoom;
  13. private AudioSource CurrentBackgroundTheme;
  14. // Sound Fx
  15. public AudioSource FxClick1;
  16. public AudioSource FxClick2;
  17. public AudioSource FxClick3;
  18. public AudioSource FxClick4;
  19. public AudioSource FxExplosion;
  20. public AudioSource FxData;
  21. public AudioSource FxSuitNoise;
  22. public AudioSource FxSquelch1;
  23. public AudioSource FxSquelch2;
  24. public AudioSource FxLaser1;
  25. public AudioSource FxLaser2;
  26. // Parameters for fading between background themes
  27. public float FadeSpeed = 0.025f;
  28. public float FadeDelay = 0.01f;
  29. public float Volume = 1f;
  30. // =========================================================================
  31. // Singleton
  32. public static AudioPlayer Instance = null;
  33. private void Awake()
  34. {
  35. if (Instance == null)
  36. {
  37. Instance = this;
  38. }
  39. else if (Instance != this)
  40. {
  41. Destroy(gameObject);
  42. }
  43. DontDestroyOnLoad(gameObject);
  44. }
  45. // =========================================================================
  46. // Public API
  47. public void PlayFxClick1() { PlayFx(FxClick1); }
  48. public void PlayFxClick2() { PlayFx(FxClick2); }
  49. public void PlayFxClick3() { PlayFx(FxClick3); }
  50. public void PlayFxClick4() { PlayFx(FxClick4); }
  51. public void PlayFxExplosion() { PlayFx(FxExplosion); }
  52. public void PlayFxData() { PlayFx(FxData); }
  53. public void PlayFxSuitNoise() { PlayFx(FxSuitNoise); }
  54. public void PlayFxSquelch1() { PlayFx(FxSquelch1); }
  55. public void PlayFxSquelch2() { PlayFx(FxSquelch2); }
  56. public void PlayFxLaser1() { PlayFx(FxLaser1); }
  57. public void PlayFxLaser2() { PlayFx(FxLaser2); }
  58. public void PlayBackgroundThemeBotanicalRoom()
  59. {
  60. PlayBackgroundTheme(BackgroundThemeBotanicalRoom);
  61. }
  62. public void PlayBackgroundThemeMachineRoom()
  63. {
  64. PlayBackgroundTheme(BackgroundThemeMachineRoom);
  65. }
  66. public void PlayBackgroundThemeCryoRoom()
  67. {
  68. PlayBackgroundTheme(BackgroundThemeCryoRoom);
  69. }
  70. public void PlayBackgroundThemeKitchenRoom()
  71. {
  72. PlayBackgroundTheme(BackgroundThemeKitchenRoom);
  73. }
  74. public void PlayBackgroundThemeControlRoom()
  75. {
  76. PlayBackgroundTheme(BackgroundThemeControlRoom);
  77. }
  78. // =========================================================================
  79. // Internal control
  80. private void PlayFx(AudioSource fx)
  81. {
  82. if (fx.isPlaying)
  83. {
  84. fx.Stop();
  85. fx.time = 0;
  86. }
  87. fx.Play();
  88. }
  89. private void PlayBackgroundTheme(AudioSource backgroundTheme)
  90. {
  91. StartCoroutine(
  92. FadeNextBackgroundTheme(
  93. CurrentBackgroundTheme,
  94. backgroundTheme,
  95. FadeSpeed,
  96. FadeDelay,
  97. Volume
  98. )
  99. );
  100. CurrentBackgroundTheme = backgroundTheme;
  101. }
  102. // Background theme fade in/ fade out
  103. static IEnumerator FadeNextBackgroundTheme(
  104. AudioSource current,
  105. AudioSource next,
  106. float fadeSpeed,
  107. float fadeDelay,
  108. float volume
  109. )
  110. {
  111. // Fade out current clip, if neccessary
  112. if (current != null)
  113. {
  114. while (current.volume > 0)
  115. {
  116. current.volume -= fadeSpeed * volume;
  117. yield return new WaitForSeconds(fadeDelay);
  118. }
  119. current.Stop();
  120. }
  121. // Fade in next clip
  122. next.loop = true;
  123. next.volume = 0;
  124. next.Play();
  125. while (next.volume < volume)
  126. {
  127. next.volume += fadeSpeed * volume;
  128. yield return new WaitForSeconds(fadeDelay);
  129. }
  130. }
  131. }