// *********************************************************************** // Assembly : Unity // Author : Kimch // Created : // // Last Modified By : Kimch // Last Modified On : // *********************************************************************** // // // *********************************************************************** #if !UNITY_EDITOR && SDK_WECHAT_WASM #define DISABLE_UNITY_AUDIO #endif using System; using UnityEngine; using UnityEngine.Audio; namespace G { /// /// Class Sound Manager Aduio or Music /// [RequireComponent(typeof(AudioSource))] public class SoundProxy : F.GameProxy { private const string kFxEnable = "sound_fx"; private const string kBgEnable = "sound_bg"; private const string kFxVolume = "fx_volume"; private const string kBgVolume = "bg_volume"; public const byte BG_LAYER_INDEX = 1; public const byte FX_LAYER_INDEX = 2; private bool _fxEnabled = true; private bool _bgEnabled = true; private float _bgVolume = 1f; private float _fxVolume = 1f; #if !DISABLE_UNITY_AUDIO private AudioMixer _audioMixer; private AudioMixerGroup[] _audioMixerGroups; #endif private AudioSource _audioSource; private AudioSource _bgSoundLayer; private AudioSource _fxSoundLayer; private static float GetDBVolume(float volume) { return volume * 80f - 80f; } /// /// Gets or sets a value indicating whether [audio enable]. /// /// /// true if [audio enable]; otherwise, false. /// public bool fxEnabled { get { return _fxEnabled; } set { if (_fxEnabled != value) { _fxEnabled = value; #if !DISABLE_UNITY_AUDIO _audioMixer.SetFloat(kFxVolume, value ? GetDBVolume(_fxVolume) : GetDBVolume(0f)); #endif PlayerPrefs.SetInt(kFxEnable, value ? 1 : 0); PlayerPrefs.Save(); } } } /// /// Gets or sets a value indicating whether [music enable]. /// /// /// true if [music enable]; otherwise, false. /// public bool bgEnabled { get { return _bgEnabled; } set { if (_bgEnabled != value) { _bgEnabled = value; #if !DISABLE_UNITY_AUDIO _audioMixer.SetFloat(kBgVolume, value ? GetDBVolume(_bgVolume) : GetDBVolume(0f)); #else KPlatform.Instance.PauseMusic(!value); #endif PlayerPrefs.SetInt(kBgEnable, value ? 1 : 0); PlayerPrefs.Save(); } } } public float fxVolume { get { return _fxVolume; } set { _fxVolume = value; #if !DISABLE_UNITY_AUDIO _audioMixer.SetFloat(kFxVolume, GetDBVolume(value)); #endif PlayerPrefs.SetFloat(kFxVolume, value); PlayerPrefs.Save(); } } public float bgVolume { get { return _bgVolume; } set { _bgVolume = value; #if !DISABLE_UNITY_AUDIO _audioMixer.SetFloat(kBgVolume, GetDBVolume(value)); #endif PlayerPrefs.SetFloat(kBgVolume, value); PlayerPrefs.Save(); } } /// /// Does the play audio. /// /// The clip. private void DoPlayFx(AudioClip clip) { if (_fxEnabled) { _fxSoundLayer.loop = false; _fxSoundLayer.PlayOneShot(clip); } } /// /// Does the stop music. /// private void DoStopBg() { if (_bgSoundLayer) { _bgSoundLayer.Stop(); } } /// /// Does the play music. /// /// The clip. /// if set to true [sync time]. private void DoPlayBg(AudioClip clip, bool syncTime) { if (_bgSoundLayer.isPlaying && clip == _bgSoundLayer.clip) { return; } _bgSoundLayer.clip = clip; _bgSoundLayer.Play(); } private AudioSource CreateSoundLayer(int group = 0) { var soundLayerGO = new GameObject(nameof(SoundLayer)); soundLayerGO.transform.parent = this.transform; #if UNITY_EDITOR && !DISABLE_UNITY_AUDIO if (group < _audioMixerGroups.Length) soundLayerGO.name = _audioMixerGroups[group].name; #endif var result = soundLayerGO.AddComponent(); #if !DISABLE_UNITY_AUDIO if (group < _audioMixerGroups.Length) result.outputAudioMixerGroup = _audioMixerGroups[group]; #endif if (group == BG_LAYER_INDEX) result.loop = true; return result; } /// /// /// /// /// BG_LAYER_INDEX FX_LAYER_INDEX public void SetAudioMixer(AudioSource audioSource, int group) { #if !DISABLE_UNITY_AUDIO if (audioSource && _audioMixerGroups != null && group < _audioMixerGroups.Length) { audioSource.outputAudioMixerGroup = _audioMixerGroups[group]; } #endif } #region Unity /// /// Awakes this instance. /// private void Awake() { Instance = this; } private void Start() { #if !DISABLE_UNITY_AUDIO _audioMixer = Resources.Load("GameAudioMixer"); if (!_audioMixer) return; _audioMixerGroups = _audioMixer.FindMatchingGroups("Master"); if (_audioMixerGroups == null) return; #endif _bgSoundLayer = CreateSoundLayer(BG_LAYER_INDEX); _fxSoundLayer = CreateSoundLayer(FX_LAYER_INDEX); this.bgVolume = PlayerPrefs.GetFloat(kBgVolume, 1f); this.fxVolume = PlayerPrefs.GetFloat(kFxVolume, 1f); this.bgEnabled = (PlayerPrefs.GetInt(kBgEnable, 1) == 1); this.fxEnabled = (PlayerPrefs.GetInt(kFxEnable, 1) == 1); } private void LateUpdate() { //性能问题 //if (Camera.main) //{ // this.transform.position = Camera.main.transform.position; //} } #endregion #region Static public static SoundProxy Instance; public static void PlayFx(AudioClip clip) { if (Instance && clip) { Instance.DoPlayFx(clip); } } /// /// /// /// public static async void PlayFxAsync(string name) { if (string.IsNullOrEmpty(name)) return; #if !DISABLE_UNITY_AUDIO string key = System.IO.Path.GetFileNameWithoutExtension(name); var audioClip = await AssetProxy.Instance.TryGetTemporaryAssetAsync(key).Task; if (Instance && audioClip) { Instance.DoPlayFx(audioClip); } #else string key = System.IO.Path.GetFileName(name); key = System.IO.Path.ChangeExtension(key, ".mp3"); Debug.Log("sfx " + key); if (Instance && Instance.fxEnabled) KPlatform.Instance.PlayAudio("AGame/Res/Sounds/" + key, Instance.fxVolume); #endif } /// /// /// /// /// public static async void PlayBgAsync(string name, bool syncTime = false) { if (string.IsNullOrEmpty(name)) return; #if UNITY_EDITOR string checkFile = System.IO.Path.GetFileName(name); checkFile = "Assets/AGame/Res/Sounds/" + System.IO.Path.ChangeExtension(checkFile, ".mp3"); if (!System.IO.File.Exists(checkFile)) { Debug.Log(StringLoggingExtensions.Colored(checkFile, Color.red)); } #endif #if !DISABLE_UNITY_AUDIO string key = System.IO.Path.GetFileNameWithoutExtension(name); var audioClip = await AssetProxy.Instance.TryGetTemporaryAssetAsync(key).Task; if (Instance && audioClip) { Instance.DoPlayBg(audioClip, syncTime); } #else string key = System.IO.Path.GetFileName(name); key = System.IO.Path.ChangeExtension(key, ".mp3"); //Debug.Log("sbg " + key); if (Instance && Instance.bgEnabled) KPlatform.Instance.PlayMusic("AGame/Res/Sounds/" + key, Instance.bgVolume); #endif } #endregion } }