private void UpdateSoundSource(int index) { SoundSourceCtrl ctrl = scourceCtrls[index]; if (ctrl != null && ctrl.status != SoundSourceCtrl.BGMSourceStatus.Slient && ctrl.status != SoundSourceCtrl.BGMSourceStatus.Play) { if (ctrl.status == SoundSourceCtrl.BGMSourceStatus.FadeIn) { ctrl.audioSoure.volume += 0.1f; if (ctrl.audioSoure.volume >= Volume) { ctrl.audioSoure.volume = Volume; ctrl.status = SoundSourceCtrl.BGMSourceStatus.Play; } } if (ctrl.status == SoundSourceCtrl.BGMSourceStatus.FadeOut) { ctrl.audioSoure.volume -= 0.1f; if (ctrl.audioSoure.volume <= 0f) { ctrl.audioSoure.volume = 0f; ctrl.status = SoundSourceCtrl.BGMSourceStatus.Slient; ctrl.audioSoure.Stop(); } } } }
public void Awake() { mIsOn = bool.Parse(PlayerPrefs.GetString("BGMMgrIsON", "true")); Volume = PlayerPrefs.GetFloat("BGMVolume", 0.3f); Instance = this; scourceCtrls[0] = new SoundSourceCtrl(0); scourceCtrls[1] = new SoundSourceCtrl(1); }
/// <summary> /// 寻找一个正在淡入的声音组件 /// </summary> /// <returns></returns> private int FetchASourceIndex() { int ret = 0; for (int i = 0; i < scourceCtrls.Length; i++) { SoundSourceCtrl ctrl = scourceCtrls[i]; if (ctrl.status == SoundSourceCtrl.BGMSourceStatus.Slient || ctrl.status == SoundSourceCtrl.BGMSourceStatus.FadeIn) { ret = i; break; } } return(ret); }
public void PlayBGMAsync(string strBGMName) { currentMusicName = strBGMName; if (!IsOn || string.IsNullOrEmpty(strBGMName)) { return; } int index = FetchASourceIndex(); SoundSourceCtrl srcCtrl = scourceCtrls[index]; srcCtrl.curMusicName = strBGMName; ResMgr.Intstance.LoadAsset(strBGMName, typeof(AudioClip), a => { if (Instance.scourceCtrls[index].curMusicName.Equals(strBGMName)) { Instance.scourceCtrls[index].InitFadeIn(a as AudioClip); Instance.scourceCtrls[1 - index].InitFadeOut(); } }); }