123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using ET;
- using ET.Client;
- using ET.EventType;
- using System.Text.RegularExpressions;
- using UnityEngine;
- [Event(SceneType.Client)]
- public class PlayBGMHandler : BEvent<PlayBgm>
- {
- protected override async ETTask OnEvent(PlayBgm args)
- {
- if (args.Play)
- {
- SoundManager.Instance.PlayBgm(args.Bgm).Coroutine();
- }
- else
- {
- SoundManager.Instance.StopBgm();
- }
- await ETTask.CompletedTask;
- }
- }
- public class SoundManager : Singleton<SoundManager>, ISingletonAwake
- {
- private AudioSource mMusicBgAudioSource = null;
- private AudioSource mSoundAudioSource = null;
- public bool BGMute
- {
- get { return mMusicBgAudioSource.mute; }
- set { mMusicBgAudioSource.mute = value; }
- }
- public float BGVolume
- {
- get { return mMusicBgAudioSource.volume; }
- set { mMusicBgAudioSource.volume = value; }
- }
- public bool MusicMute
- {
- get { return mSoundAudioSource.mute; }
- set { mSoundAudioSource.mute = value; }
- }
- public float MusicVolume
- {
- get { return mSoundAudioSource.volume; }
- set { mSoundAudioSource.volume = value; }
- }
- public void Awake()
- {
- Log.Debug("初始化SoundManager管理");
- var globalObj = GameObject.Find("Global");
- GameObject bgMusicObj = new GameObject("BGMusicObj");
- bgMusicObj.transform.parent = globalObj.transform;
- mMusicBgAudioSource = bgMusicObj.AddComponent<AudioSource>();
- mMusicBgAudioSource.loop = true;
- mMusicBgAudioSource.volume = 1f;
- mMusicBgAudioSource.playOnAwake = false;
- var mute = GameSetting.Instance.GetBool(GameSetting.MusicSets.Mute_BG, GameSetting.BGMusicMute);
- mMusicBgAudioSource.mute = mute;
- GameObject soundMusicObj = new GameObject("SoundMusicObj");
- soundMusicObj.transform.parent = globalObj.transform;
- mSoundAudioSource = soundMusicObj.AddComponent<AudioSource>();
- mSoundAudioSource.loop = false;
- mSoundAudioSource.volume = 1f;
- mSoundAudioSource.playOnAwake = false;
- mute = GameSetting.Instance.GetBool(GameSetting.MusicSets.Mute_Music, GameSetting.MusicMute);
- mSoundAudioSource.mute = mute;
- }
- public async ETTask PlaySound(string name,bool isLoop = false)
- {
- if (isLoop)
- {
- await PlayBgm(name);
- }
- else
- {
- await PlayUISound(name);
- }
- }
- public async ETTask PlayBgm(string name)
- {
- if (BGMute) return;
- Log.Debug($"play bgm:{name}");
- var ac = await GameObjectPool.Instance.AcquireSound(name);
- mMusicBgAudioSource.clip = ac;
- mMusicBgAudioSource.loop = true;
- mMusicBgAudioSource.Play();
- }
- public void StopBgm()
- {
- Log.Debug("stop bgm");
- mMusicBgAudioSource.Stop();
- }
- public async ETTask PlayUISound(string name, float duration = -1f)
- {
- if (MusicMute) return;
- var ac = await GameObjectPool.Instance.AcquireSound(name);
- mSoundAudioSource.loop = false;
- mSoundAudioSource.PlayOneShot(ac);
- }
- }
|