private void Update() { switch (currentState) { case audioManagerState.notPlaying: if (thisHasStartedPlaying) { if (musicAudioSource.volume < musicMaxVolume) { musicAudioSource.volume += 0.01f; //Fade in the music } else { musicAudioSource.volume = musicMaxVolume; thisHasStartedPlaying = false; currentState = audioManagerState.playing; } } break; case audioManagerState.switching: if (!thisHasStartedPlaying) //Fade out the current music first { if (musicAudioSource.volume > 0.0f) { musicAudioSource.volume -= 0.1f; } else { musicAudioSource.volume = 0.0f; //Fade out the music musicAudioSource.Stop(); //Stop playing the old piece musicAudioSource.clip = playThisNext; //Update the clip thisHasStartedPlaying = true; //Fade in the new music musicAudioSource.Play(); //Start playing the new piece } } else { if (musicAudioSource.volume < musicMaxVolume) { musicAudioSource.volume += 0.05f; //Fade in the music } else { musicAudioSource.volume = musicMaxVolume; thisHasStartedPlaying = false; currentState = audioManagerState.playing; //Fade in complete } } break; } }
public void PlayThisClip(string clipName) { switch (clipName) //Get the new clip { case "TitleTheme": playThisNext = titleTheme1; break; case "WorldMapMusic1": playThisNext = worldMapMusic1; previousSong = "WorldMapMusic1"; break; case "BattleMusic1": playThisNext = battleMusic1; break; case "BossMusic1": playThisNext = bossMusic1; break; case "VictoryMusic1": playThisNext = victoryMusic1; break; case "DefeatMusic1": playThisNext = defeatMusic1; break; case "Cutscene1": playThisNext = cutscene1; break; case "TownTheme": playThisNext = townTheme1; previousSong = "TownTheme"; break; case "Dommel": playThisNext = dommelHouse; previousSong = "Dommel"; break; case "Brenna": playThisNext = brennaHouse; previousSong = "Brenna"; break; case "End": playThisNext = creditsTheme; break; } if (currentState == audioManagerState.notPlaying) //The very first call { thisHasStartedPlaying = true; musicAudioSource.clip = playThisNext; musicAudioSource.Play(); } else if (currentState == audioManagerState.playing) //If we are already playing something, go to switching { currentState = audioManagerState.switching; thisHasStartedPlaying = false; } }