/// <summary> /// Starts playing the playlist. /// </summary> /// <param name="delay">Seconds of delaying the start of the next track.</param> /// <param name="fadeDuration">Seconds the fade will took.</param> public void Play(int delay = 0, float fadeDuration = 3f) { AudioClip clip = GetRandomClip(_audioSource.Clip); if (_audioSource.IsPlaying) { _audioSource.CrossFadeToNewClip(clip); } else { _audioSource.FadeIn(clip, duration: fadeDuration, delay: delay); } }
/// <summary> /// Plays the sound when the object is instantiated. /// </summary> private void PlayOnAwake() { _audioSource.IsLoop = true; if (_audioSource.IsPlaying || ambientClip == null) { return; } float clipLength = ambientClip.length; float startTime = Random.Range(0f, clipLength); _audioSource.MixerGroup = mixerGroup; _audioSource.FadeIn(ambientClip, startTime: startTime, rollOffMaxDistance: rollOffMaxDistance, targetVolume: targetVolume); }