324 lines
7.6 KiB
C#
324 lines
7.6 KiB
C#
// ***********************************************************************
|
|
// Assembly : Unity
|
|
// Author : Kimch
|
|
// Created :
|
|
//
|
|
// Last Modified By : Kimch
|
|
// Last Modified On :
|
|
// ***********************************************************************
|
|
// <copyright file= "SoundProxy" company=""></copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
#if !UNITY_EDITOR && SDK_WECHAT_WASM
|
|
#define DISABLE_UNITY_AUDIO
|
|
#endif
|
|
|
|
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.Audio;
|
|
|
|
namespace G
|
|
{
|
|
/// <summary>
|
|
/// Class Sound Manager Aduio or Music
|
|
/// </summary>
|
|
[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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether [audio enable].
|
|
/// </summary>
|
|
/// <value>
|
|
/// <c>true</c> if [audio enable]; otherwise, <c>false</c>.
|
|
/// </value>
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether [music enable].
|
|
/// </summary>
|
|
/// <value>
|
|
/// <c>true</c> if [music enable]; otherwise, <c>false</c>.
|
|
/// </value>
|
|
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();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Does the play audio.
|
|
/// </summary>
|
|
/// <param name="clip">The clip.</param>
|
|
private void DoPlayFx(AudioClip clip)
|
|
{
|
|
if (_fxEnabled)
|
|
{
|
|
_fxSoundLayer.loop = false;
|
|
_fxSoundLayer.PlayOneShot(clip);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Does the stop music.
|
|
/// </summary>
|
|
private void DoStopBg()
|
|
{
|
|
if (_bgSoundLayer)
|
|
{
|
|
_bgSoundLayer.Stop();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Does the play music.
|
|
/// </summary>
|
|
/// <param name="clip">The clip.</param>
|
|
/// <param name="syncTime">if set to <c>true</c> [sync time].</param>
|
|
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<AudioSource>();
|
|
#if !DISABLE_UNITY_AUDIO
|
|
if (group < _audioMixerGroups.Length)
|
|
result.outputAudioMixerGroup = _audioMixerGroups[group];
|
|
#endif
|
|
if (group == BG_LAYER_INDEX)
|
|
result.loop = true;
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="audioSource"></param>
|
|
/// <param name="group">BG_LAYER_INDEX FX_LAYER_INDEX</param>
|
|
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
|
|
|
|
/// <summary>
|
|
/// Awakes this instance.
|
|
/// </summary>
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
#if !DISABLE_UNITY_AUDIO
|
|
_audioMixer = Resources.Load<AudioMixer>("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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
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<AudioClip>(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
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <param name="syncTime"></param>
|
|
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<AudioClip>(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
|
|
}
|
|
}
|