// ------------------------------------------------------------------------------- public void PlayBGMusic(string name) { if (mCurrentBGMusicData != null && name == mCurrentBGMusicData.Name) { return; } if (mIsCrossFadingBGMusic) { Debug.LogError("Sorry, can't change background music while in the middle of a cross-fade!", gameObject); } BGMusicData nextBGMusicData; if (mBGMusicDataDict.TryGetValue(name, out nextBGMusicData)) { mIsCrossFadingBGMusic = true; var currentBGMusicAudioSource = (mCurrentBGMusicSourceIsA) ? BGMusicAudioSourceA : BGMusicAudioSourceB; var nextBGMusicAudioSource = (mCurrentBGMusicSourceIsA) ? BGMusicAudioSourceB : BGMusicAudioSourceA; // Start playing the new clip but fade the volume in from 0 nextBGMusicAudioSource.clip = nextBGMusicData.AudioClip; nextBGMusicAudioSource.volume = 0.0f; nextBGMusicAudioSource.Play(); nextBGMusicAudioSource.time = (nextBGMusicData.ContinueFromLastPlayback) ? nextBGMusicData.LastPlaybackTime : 0.0f; LeanTween.value(0.0f, 1.0f, BGMusicCrossFadeDuration).setOnUpdate((float value) => { currentBGMusicAudioSource.volume = (1.0f - value); nextBGMusicAudioSource.volume = (value); }).setOnComplete(() => { if (mCurrentBGMusicData != null) { mCurrentBGMusicData.LastPlaybackTime = currentBGMusicAudioSource.time; } mCurrentBGMusicData = nextBGMusicData; currentBGMusicAudioSource.Stop(); mCurrentBGMusicSourceIsA = !mCurrentBGMusicSourceIsA; mIsCrossFadingBGMusic = false; }); } else { Debug.LogError("Couldn't find backgroud music with the name: " + name); } }
static SoundManager() { Dictionary <BackgroundMusicType, BGMusicData> dictionary = new Dictionary <BackgroundMusicType, BGMusicData>(); BGMusicData data = new BGMusicData { path = "uibg", volume = 1f }; dictionary.Add(BackgroundMusicType.eMain, data); data = new BGMusicData { path = "battlebg", volume = 0.5f }; dictionary.Add(BackgroundMusicType.eBattle, data); mBGList = dictionary; }