/// <summary> /// 播放音效 /// </summary> void PlaySound(AudioSoundMsg soundMsg) { if (SoundOn.Value) { AudioUnit unit = SafeObjectPool <AudioUnit> .Instance.Allocate(); unit.SetOnStartListener(delegate(AudioUnit soundUnit) { soundMsg.onSoundBeganCallback.InvokeGracefully(); unit.SetVolume(soundMsg.Volume); unit.SetOnStartListener(null); }); unit.SetAudio(gameObject, soundMsg.SoundName, false); unit.SetOnFinishListener(delegate(AudioUnit soundUnit) { soundMsg.onSoundEndedCallback.InvokeGracefully(); unit.SetOnFinishListener(null); }); } }
/// <summary> /// 播放音乐 /// </summary> void PlayMusic(AudioMusicMsg musicMsg) { if (!MusicOn && musicMsg.allowMusicOff) { musicMsg.onMusicBeganCallback.InvokeGracefully(); musicMsg.onMusicEndedCallback.InvokeGracefully(); return; } Log.I(">>>>>> Start Play Music"); // TODO: 需要按照这个顺序去 之后查一下原因 //需要先注册事件,然后再play mMainUnit.SetOnStartListener(delegate(AudioUnit musicUnit) { musicMsg.onMusicBeganCallback.InvokeGracefully(); //调用完就置为null,否则应用层每注册一个而没有注销,都会调用 mMainUnit.SetOnStartListener(null); }); mMainUnit.SetAudio(gameObject, musicMsg.MusicName, musicMsg.Loop); mMainUnit.SetOnFinishListener(delegate(AudioUnit musicUnit) { musicMsg.onMusicEndedCallback.InvokeGracefully(); //调用完就置为null,否则应用层每注册一个而没有注销,都会调用 mMainUnit.SetOnFinishListener(null); }); }
/// <summary> /// 播放语音 /// </summary> void PlayVoice(AudioVoiceMsg msg) { mVoiceUnit.SetOnStartListener(delegate(AudioUnit musicUnit) { // SetVolume(mVoiceUnit, VolumeLevel.Min); msg.onVoiceBeganCallback.InvokeGracefully(); mVoiceUnit.SetOnStartListener(null); }); mVoiceUnit.SetAudio(gameObject, msg.voiceName, msg.loop); mVoiceUnit.SetOnFinishListener(delegate(AudioUnit musicUnit) { // SetVolume(mVoiceUnit, VolumeLevel.Max); msg.onVoiceEndedCallback.InvokeGracefully(); mVoiceUnit.SetOnFinishListener(null); }); }