private AudioSource GetAvailableSource(AudioFileSettings file) { //return null if the sound provided is already playing if (_sources.Exists(source => source.isPlaying && source.clip != file.clip)) { return(null); } //gather the sources that are not playing var notPlaying = _sources.FindAll(source => !source.isPlaying); for (var index = 0; index < notPlaying.Count; index++) { var t = notPlaying[index]; notPlaying.Remove(t); DestroySource(t); } if (notPlaying.Count != 0) { return(notPlaying[0]); } { var source = _soundSourceContainer.AddComponent <AudioSource>(); source.playOnAwake = false; _sources.Add(source); return(source); } }
private void Update() { if (queued && !source.isPlaying) { PlaySound(queued); queued = null; } }
public void StopSound(AudioFileSettings file) { var sound = _sources.Where(x => x.clip == file.clip); foreach (var audioSource in sound) { Destroy(audioSource); } }
public void Queue(AudioFileSettings file = null) { if (!file) { file = initialBGM; } queued = file; source.loop = false; }
private void PlaySound(AudioFileSettings audio) { source.clip = audio.clip; source.volume = audio.Volume; source.pitch = audio.Pitch; source.priority = audio.Priority; source.loop = true; source.outputAudioMixerGroup = audio.mixerGroup; source.Play(); }
public void PlaySound(AudioFileSettings file) { var source = GetAvailableSource(file); if (!source) { return; } source.clip = file.clip; source.outputAudioMixerGroup = file.mixerGroup; source.volume = file.Volume; source.pitch = file.Pitch; source.priority = file.Priority; source.loop = file.Loop; source.outputAudioMixerGroup = file.mixerGroup; source.Play(); }