SoundController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, Transform parent = null)
  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, parent);
  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, Transform parent = null)
  67. {
  68. if(parent == null) parent = soundsParent.transform;
  69. if(SoundController.instance == null)
  70. {
  71. Debug.LogError("Missing SoundController component in scene.");
  72. return;
  73. }
  74. if(clip.sfxType == SFXClipType.Steps)
  75. {
  76. SoundController ctrl = SoundController.instance;
  77. foreach (var step in clip.clipSteps)
  78. {
  79. ctrl.StartCoroutine(ctrl.EmmitSoundInFuture(step.startTime, step.clip, position));
  80. }
  81. }
  82. else
  83. {
  84. GameObject soundEmitterObj;
  85. if(instance.soundEmitterPrefab != null) soundEmitterObj = GameObject.Instantiate(instance.soundEmitterPrefab, parent);
  86. else soundEmitterObj = MasterObjectPooler.Instance.GetPool(soundEmitterPool).GetObject(position);
  87. soundEmitterObj.transform.parent = parent;
  88. SoundEmitter soundEmitter = soundEmitterObj.GetComponent<SoundEmitter>();
  89. soundEmitter.Initialize(clip, position);
  90. soundEmitters.Add(soundEmitter);
  91. }
  92. }
  93. public IEnumerator EmmitSoundInFuture(float waitTime, SFXClip clip, Vector3 position)
  94. {
  95. yield return new WaitForSeconds(waitTime);
  96. EmmitSound(clip, position);
  97. }
  98. }
  99. }