public void Play(string name, bool?keepTimestamp = null) { CrossfadeMusicPlayerObject target = Tracks.Find(a => a.Name == name); if (target == null) { throw new System.Exception("No matching audio clip! (" + name + ")"); } if (mainAudioSource.clip == target.AudioClip || name == Playing) { return; } seconderyAudioSource.loop = true; playingIntro = false; seconderyAudioSource.clip = target.AudioClip; Playing = name; mainAudioSource.volume = Volume; seconderyAudioSource.volume = 0; count = 0; if (keepTimestamp ?? KeepTimestamp) { seconderyAudioSource.time = mainAudioSource.time * (seconderyAudioSource.clip.length / mainAudioSource.clip.length); } else { seconderyAudioSource.time = 0; } seconderyAudioSource.Play(); }
private void Update() { if (seconderyAudioSource.clip != null) { count += Time.unscaledDeltaTime * FadeSpeed; if (count >= 1) { AudioSource temp = mainAudioSource; mainAudioSource = seconderyAudioSource; seconderyAudioSource = temp; mainAudioSource.volume = Volume; seconderyAudioSource.volume = 0; seconderyAudioSource.clip = null; seconderyAudioSource.Stop(); count = 0; } else { mainAudioSource.volume = Volume * (1 - count); seconderyAudioSource.volume = Volume * count; } } if (playingIntro && !mainAudioSource.isPlaying) { Playing = Playing.Replace("Intro", ""); CrossfadeMusicPlayerObject target = Tracks.Find(a => a.Name == Playing); if (target == null) { throw new System.Exception("No main track for the intro! (" + Playing + ")"); } mainAudioSource.clip = target.AudioClip; mainAudioSource.Play(); playingIntro = false; mainAudioSource.loop = true; } }