SoundController.cs 4.1 KB

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