#region Copyright RenGuiYou. All rights reserved. //===================================================== // NeatlyFrameWork // Author: RenGuiyou // Feedback: mailto:750539605@qq.com //===================================================== #endregion using Neatly.Load.Manager; using Neatly.UI; using UnityEngine; namespace Neatly.Module { public class SoundModule : ModuleSingleton { private float m_MusicVolume; private float m_SoundVolume; private bool m_MusicMute; private bool m_SoundMute; private string m_LastMusicName = string.Empty; private AudioSource m_MusicSource; private AudioSource[] m_SoundSources; public override void Init() { //获取序列号值 m_MusicVolume = PlayerPrefsHelper.GetMusicVolume(); m_MusicMute = PlayerPrefsHelper.GetMusicMute(); m_SoundVolume = PlayerPrefsHelper.GetSoundVolume(); m_SoundMute = PlayerPrefsHelper.GetSoundMute(); //初始化音乐Source m_MusicSource = NeatlyFrame.Instance.audioRoot.AddComponent(); m_MusicSource.volume = m_MusicVolume; m_MusicSource.mute = m_MusicMute; m_MusicSource.loop = true; //初始化音效Source m_SoundSources = new AudioSource[NeatlyConfig.SOUND_SOURCE_COUNT]; for (int i = 0; i < m_SoundSources.Length; i++) { m_SoundSources[i] = NeatlyFrame.Instance.audioRoot.AddComponent(); m_SoundSources[i].volume = m_SoundVolume; m_SoundSources[i].mute = m_SoundMute; m_SoundSources[i].loop = false; } NeatlyUI.RegisterPlaySoundAction(PlaySound); } public float MusicVolume { get { return m_MusicVolume; } set { if (!SetProperty.SetValue(ref m_MusicVolume, value)) return; PlayerPrefsHelper.SetMusicVolume(m_MusicVolume); m_MusicSource.volume = m_MusicVolume; } } public float SoundVolume { get { return m_SoundVolume; } set { if (!SetProperty.SetValue(ref m_SoundVolume, value)) return; PlayerPrefsHelper.SetSoundVolume(m_SoundVolume); for (int i = 0; i < m_SoundSources.Length; i++) { m_SoundSources[i].volume = m_SoundVolume; } } } public bool MusicMute { get { return m_MusicMute; } set { if (!SetProperty.SetValue(ref m_MusicMute, value)) return; PlayerPrefsHelper.SetMusicMute(m_MusicMute); m_MusicSource.mute = m_MusicMute; } } public bool SoundMute { get { return m_SoundMute; } set { if (!SetProperty.SetValue(ref m_SoundMute, value)) return; PlayerPrefsHelper.SetSoundMute(m_SoundMute); for (int i = 0; i < m_SoundSources.Length; i++) { m_SoundSources[i].mute = m_SoundMute; } } } public void ReplayMusic() { m_MusicSource.Stop(); m_MusicSource.Play(); } public void PlayMusic(string name) { if (SetProperty.SetValue(ref m_LastMusicName, name)) { m_MusicSource.clip = SoundLoadManager.Instance.LoadMusic(name); m_MusicSource.enabled = true; m_MusicSource.Play(); } } public void StopMusic() { if (m_MusicSource.isPlaying) { m_MusicSource.Stop(); m_MusicSource.clip = null; } } public void PauseMusic() { m_MusicSource.Pause(); } public void UnPauseMusic() { m_MusicSource.UnPause(); } public void PlaySound(string name) { PlaySound(name, false); } public void PlaySound(string name, bool compossibility) { if (string.IsNullOrEmpty(name)) name = NeatlyConfig.NAME_DEFAULT_SOUND; if (!compossibility) { for (int i = 0; i < m_SoundSources.Length; i++) { if (m_SoundSources[i].isPlaying && m_SoundSources[i].clip != null && m_SoundSources[i].clip.name.Equals(name)) { m_SoundSources[i].Stop(); m_SoundSources[i].Play(); return; } } } int m_soundIndex = FindFreeSource(); m_SoundSources[m_soundIndex].Stop(); m_SoundSources[m_soundIndex].clip = SoundLoadManager.Instance.LoadSound(name); m_SoundSources[m_soundIndex].Play(); } public void StopSound() { for (int i = 0; i < m_SoundSources.Length; i++) { if (m_SoundSources[i].isPlaying) { m_SoundSources[i].Stop(); } } } public int FindFreeSource() { for (int i = 0; i < m_SoundSources.Length; i++) { if (!m_SoundSources[i].isPlaying) { return i; } } return 0; } } }