using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Audio; using Sirenix.OdinInspector; using QFSW.MOP2; namespace KairoEngine.SFX { // Todo: Remove old EmmitSound function. /// /// Controls playing SFX clips. /// [HideMonoScript] public class SoundController : SerializedMonoBehaviour { [Tooltip("The prefab that will be used as the sound emitter. It needs a audio source and a sphere colllider.")] public GameObject soundEmitterPrefab; public static string soundEmitterPool; public string _soundEmitterPool; public static GameObject soundsParent; [Tooltip("Container GameObject for the instantiated sound emitter prefabs.")] public GameObject _soundsParent; [Tooltip("A dictionary of sound SFX clips and their names"), InlineEditor(InlineEditorObjectFieldModes.Boxed)] public SFXLibrary soundLibrary; [ShowInInspector, Tooltip("A list of instantiated sound emitters. They are deleted when their audio stops playing.")] public static List soundEmitters = new List(); public static SoundController instance; public void Start() { //soundEmitterPrefab = _soundEmitterPrefab; soundEmitterPool = _soundEmitterPool; soundsParent = _soundsParent; SoundController.instance = this; } public static void EmmitSound(AudioClip clip, Vector3 position, float volume, float radius, string audioMixerGroupName, bool world = true) { // AudioMixer mixer = intance.soundEffectsMixer as AudioMixer; // if(mixer == null) // { // Debug.Log("Could not find asset SoundEffectsMixer"); // return; // } GameObject soundEmitterObj; if(instance.soundEmitterPrefab != null) soundEmitterObj = GameObject.Instantiate(instance.soundEmitterPrefab, soundsParent.transform); else soundEmitterObj = MasterObjectPooler.Instance.GetPool(soundEmitterPool).GetObject(position); soundEmitterObj.transform.parent = soundsParent.transform; SoundEmitter soundEmitter = soundEmitterObj.GetComponent(); // AudioMixerGroup audioMixerGroup = mixer.FindMatchingGroups(audioMixerGroupName)[0]; soundEmitter.Initialize(clip, position, volume, radius, null, world); soundEmitters.Add(soundEmitter); } public static void EmmitSound(string clipName, Vector3 position, Transform parent = null) { if(instance == null) return; if(instance.soundLibrary == null) return; SFXClip clip = null; if (instance.soundLibrary.clips.TryGetValue(clipName, out clip)) { EmmitSound(clip, position, parent); } else { Debug.LogError($"No Sound SFX clip found in Sound Library for \'{clipName}\'"); } } public static void EmmitSound(SFXClip clip, Vector3 position, Transform parent = null) { if(parent == null) parent = soundsParent.transform; if(SoundController.instance == null) { Debug.LogError("Missing SoundController component in scene."); return; } if(clip.sfxType == SFXClipType.Steps) { SoundController ctrl = SoundController.instance; foreach (var step in clip.clipSteps) { ctrl.StartCoroutine(ctrl.EmmitSoundInFuture(step.startTime, step.clip, position)); } } else { GameObject soundEmitterObj; if(instance.soundEmitterPrefab != null) soundEmitterObj = GameObject.Instantiate(instance.soundEmitterPrefab, parent); else soundEmitterObj = MasterObjectPooler.Instance.GetPool(soundEmitterPool).GetObject(position); soundEmitterObj.transform.parent = parent; SoundEmitter soundEmitter = soundEmitterObj.GetComponent(); soundEmitter.Initialize(clip, position); soundEmitters.Add(soundEmitter); } } public IEnumerator EmmitSoundInFuture(float waitTime, SFXClip clip, Vector3 position) { yield return new WaitForSeconds(waitTime); EmmitSound(clip, position); } } }