123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- 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.
- /// <summary>
- /// Controls playing SFX clips.
- /// </summary>
- 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")]
- public Dictionary<string, SFXClip> soundLibrary = new Dictionary<string, SFXClip>();
- [ShowInInspector, Tooltip("A list of instantiated sound emitters. They are deleted when their audio stops playing.")]
- public static List<SoundEmitter> soundEmitters = new List<SoundEmitter>();
- 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<SoundEmitter>();
- // 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)
- {
- SFXClip clip = null;
- if (instance.soundLibrary.TryGetValue(clipName, out clip))
- {
- EmmitSound(clip, position);
- }
- else
- {
- Debug.LogError($"No Sound SFX clip found in Sound Library for \'{clipName}\'");
- }
- }
- public static void EmmitSound(SFXClip clip, Vector3 position)
- {
- 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, soundsParent.transform);
- else soundEmitterObj = MasterObjectPooler.Instance.GetPool(soundEmitterPool).GetObject(position);
- soundEmitterObj.transform.parent = soundsParent.transform;
- SoundEmitter soundEmitter = soundEmitterObj.GetComponent<SoundEmitter>();
- soundEmitter.Initialize(clip, position);
- soundEmitters.Add(soundEmitter);
- }
- }
- public IEnumerator EmmitSoundInFuture(float waitTime, SFXClip clip, Vector3 position)
- {
- yield return new WaitForSeconds(waitTime);
- EmmitSound(clip, position);
- }
- }
- }
|