SoundController.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Audio;
  5. using Sirenix.OdinInspector;
  6. using QFSW.MOP2;
  7. namespace KairoEngine.SFX
  8. {
  9. // Todo: Remove old EmmitSound function.
  10. /// <summary>
  11. /// Controls playing SFX clips.
  12. /// </summary>
  13. [HideMonoScript]
  14. public class SoundController : SerializedMonoBehaviour
  15. {
  16. [Tooltip("The prefab that will be used as the sound emitter. It needs a audio source and a sphere colllider.")]
  17. public GameObject soundEmitterPrefab;
  18. public static string soundEmitterPool;
  19. public string _soundEmitterPool;
  20. public static GameObject soundsParent;
  21. [Tooltip("Container GameObject for the instantiated sound emitter prefabs.")]
  22. public GameObject _soundsParent;
  23. [Tooltip("A dictionary of sound SFX clips and their names"), InlineEditor(InlineEditorObjectFieldModes.Boxed)]
  24. public SFXLibrary soundLibrary;
  25. [ShowInInspector, Tooltip("A list of instantiated sound emitters. They are deleted when their audio stops playing.")]
  26. public static List<SoundEmitter> soundEmitters = new List<SoundEmitter>();
  27. public static SoundController instance;
  28. public void Start()
  29. {
  30. //soundEmitterPrefab = _soundEmitterPrefab;
  31. soundEmitterPool = _soundEmitterPool;
  32. soundsParent = _soundsParent;
  33. SoundController.instance = this;
  34. }
  35. public static void EmmitSound(AudioClip clip, Vector3 position, float volume, float radius, string audioMixerGroupName, bool world = true)
  36. {
  37. // AudioMixer mixer = intance.soundEffectsMixer as AudioMixer;
  38. // if(mixer == null)
  39. // {
  40. // Debug.Log("Could not find asset SoundEffectsMixer");
  41. // return;
  42. // }
  43. GameObject soundEmitterObj;
  44. if(instance.soundEmitterPrefab != null) soundEmitterObj = GameObject.Instantiate(instance.soundEmitterPrefab, soundsParent.transform);
  45. else soundEmitterObj = MasterObjectPooler.Instance.GetPool(soundEmitterPool).GetObject(position);
  46. soundEmitterObj.transform.parent = soundsParent.transform;
  47. SoundEmitter soundEmitter = soundEmitterObj.GetComponent<SoundEmitter>();
  48. // AudioMixerGroup audioMixerGroup = mixer.FindMatchingGroups(audioMixerGroupName)[0];
  49. soundEmitter.Initialize(clip, position, volume, radius, null, world);
  50. soundEmitters.Add(soundEmitter);
  51. }
  52. public static void EmmitSound(string clipName, Vector3 position)
  53. {
  54. if(instance == null) return;
  55. if(instance.soundLibrary == null) return;
  56. SFXClip clip = null;
  57. if (instance.soundLibrary.clips.TryGetValue(clipName, out clip))
  58. {
  59. EmmitSound(clip, position);
  60. }
  61. else
  62. {
  63. Debug.LogError($"No Sound SFX clip found in Sound Library for \'{clipName}\'");
  64. }
  65. }
  66. public static void EmmitSound(SFXClip clip, Vector3 position)
  67. {
  68. if(SoundController.instance == null)
  69. {
  70. Debug.LogError("Missing SoundController component in scene.");
  71. return;
  72. }
  73. if(clip.sfxType == SFXClipType.Steps)
  74. {
  75. SoundController ctrl = SoundController.instance;
  76. foreach (var step in clip.clipSteps)
  77. {
  78. ctrl.StartCoroutine(ctrl.EmmitSoundInFuture(step.startTime, step.clip, position));
  79. }
  80. }
  81. else
  82. {
  83. GameObject soundEmitterObj;
  84. if(instance.soundEmitterPrefab != null) soundEmitterObj = GameObject.Instantiate(instance.soundEmitterPrefab, soundsParent.transform);
  85. else soundEmitterObj = MasterObjectPooler.Instance.GetPool(soundEmitterPool).GetObject(position);
  86. soundEmitterObj.transform.parent = soundsParent.transform;
  87. SoundEmitter soundEmitter = soundEmitterObj.GetComponent<SoundEmitter>();
  88. soundEmitter.Initialize(clip, position);
  89. soundEmitters.Add(soundEmitter);
  90. }
  91. }
  92. public IEnumerator EmmitSoundInFuture(float waitTime, SFXClip clip, Vector3 position)
  93. {
  94. yield return new WaitForSeconds(waitTime);
  95. EmmitSound(clip, position);
  96. }
  97. }
  98. }