private void OnValidate() { if (Essentials.UnityIsNull(this.audioSource)) { this.audioSource = this.GetComponent <NonSpatialAudioSource>(); } }
private IEnumerator Routine() { // Init this.currentlyPlayed = GetNextTrackIndex(); this.nextPlayed = GetNextTrackIndex(); this.currentTrack.Play(this.audioSource); if (!ReferenceEquals(this.onTrackPlay, null)) { this.onTrackPlay(this.currentTrack); } while (true) { // Fade in while (this.audioSource.time < this.currentTrack.fadeInTime) { this.audioSource.volume = Mathf.Lerp(0, 1, this.currentTrack.fadeCurve.Evaluate(Mathf.Clamp01(this.audioSource.time / this.currentTrack.fadeInTime))); // Wait one frame yield return(null); } this.audioSource.volume = 1; // Wait for playback float startBlend = this.currentTrack.clip.length - this.nextTrack.fadeInTime; while (this.audioSource.time < startBlend) { // Wait one frame and measure time yield return(null); } // Blend this.nextTrack.Play(this.audioSourceNext); if (!ReferenceEquals(this.onTrackPlay, null)) { this.onTrackPlay(this.nextTrack); } while (this.audioSource.time < this.currentTrack.clip.length) { float currentTrackLeft = this.currentTrack.clip.length - this.audioSource.time; this.audioSource.volume = Mathf.Lerp(0, 1, this.currentTrack.fadeCurve.Evaluate(Mathf.Clamp01(currentTrackLeft / this.nextTrack.fadeInTime))); this.audioSourceNext.volume = Mathf.Lerp(1, 0, this.nextTrack.fadeCurve.Evaluate(Mathf.Clamp01(currentTrackLeft / this.nextTrack.fadeInTime))); // Wait one frame yield return(null); } // Swap sources var @as = this.audioSource; this.audioSource = this.audioSourceNext; this.audioSourceNext = @as; @as.Stop(); this.currentlyPlayed = this.nextPlayed; this.nextPlayed = GetNextTrackIndex(); } }