/// <summary> /// 背景音乐渐出 /// </summary> public void FadeOutBackgroundMusic() { if (_backgroundMusic != null) { _backgroundMusic.FadeInfo = new MusicFadeInfo(MusicFadeType.FadeOut, 0); _backgroundMusic = null; } }
/// <summary> /// 背景音乐渐入 /// </summary> /// <param name="audioClip">音频</param> public void FadeInBackgroundMusic(AudioClip audioClip) { var musicChannel = CreateAudioChannel(); musicChannel.FadeInfo = new MusicFadeInfo(MusicFadeType.FadeIn, _musicVolume); FadeOutBackgroundMusic(); _backgroundMusic = musicChannel; }
/// <summary> /// 播放背景音乐 /// </summary> /// <param name="audioClip">音频</param> public void PlayBackgroundMusic(AudioClip audioClip) { var musicChannel = CreateAudioChannel(); musicChannel.Play(audioClip); StopBackgroundMusic(); _backgroundMusic = musicChannel; }
/// <summary> /// 停止背景音乐 /// </summary> public void StopBackgroundMusic() { if (_backgroundMusic != null) { _backgroundMusic.Stop(); _musicChannels.Remove(_backgroundMusic); _backgroundMusic = null; } }
/// <summary> /// 创建音乐频道 /// </summary> /// <returns></returns> public MusicChannel CreateAudioChannel() { var audioSource = gameObject.AddComponent <AudioSource>(); audioSource.playOnAwake = false; audioSource.volume = _musicVolume; audioSource.spatialBlend = 0; var musicChannel = new MusicChannel(audioSource); _musicChannels.Add(musicChannel); return(musicChannel); }