using LogPrint;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum AudioPlayType
{
///
/// 立即播发 覆盖上次播发
///
PlayeNow = 1,
///
/// 等待播发 等待相同AudioClip播放完在播放
///
PlayeWaite = 2
}
public class AudioManager
{
private static AudioManager _instance;
public static AudioManager Instance
{
get
{
if (_instance == null)
{
_instance = new AudioManager();
}
return _instance;
}
}
public Dictionary<string, AudioClip> DicAudioClip = new Dictionary<string, AudioClip>();
public Dictionary<string, AudioSource> DicAudioSource = new Dictionary<string, AudioSource>();
private AudioSource m_audio;
private AudioSource m_backSource;
private AudioListener m_listener;
private GameObject m_effectAudio;
public GameObject EffectAudio
{
get
{
if (m_effectAudio == null)
{
m_effectAudio = new GameObject();
m_effectAudio.name = "EffectAudio";
}
return m_effectAudio;
}
}
private GameObject m_effectMoveAudio;
public GameObject EffectMoveAudio
{
get
{
if (m_effectMoveAudio == null)
{
m_effectMoveAudio = new GameObject();
m_effectMoveAudio.name = "EffectMoveAudio";
MoveAudio = m_effectMoveAudio.AddComponent<AudioSource>();
}
return m_effectAudio;
}
}
AudioSource MoveAudio;
private GameObject m_backAudio;
public GameObject BackAudio
{
get
{
m_backAudio = GameObject.Find("BackAudio");
if (m_backAudio == null)
{
m_backAudio = new GameObject();
m_backAudio.name = "BackAudio";
}
if (m_backAudio.GetComponent<DontDestroy>() == null)
{
m_backAudio.AddComponent<DontDestroy>();
}
GameObject audioGo = GameObject.Find("AudioGo");
if (m_backAudio.GetComponent<AudioListener>() == null)
{
//在main场景中,AudioGo这个物体上面挂着一个Listener,这里就不要添加了
//if (audioGo == null)
//{
// m_listener = m_backAudio.AddComponent<AudioListener>();
//}
m_listener = m_backAudio.AddComponent<AudioListener>();
}
if (m_backAudio.GetComponent<AudioSource>() == null)
{
m_backSource = m_backAudio.AddComponent<AudioSource>();
}
return m_backAudio;
}
}
/// <summary>
/// 声音大小
/// </summary>
/// <param name="age"></param>
public void SetAudio(float volume)
{
IEnumerator<KeyValuePair<string, AudioSource>> enumerator = DicAudioSource.GetEnumerator();
while (enumerator.MoveNext())
{
enumerator.Current.Value.volume = volume;
}
}
/// <summary>
/// 静音
/// </summary>
public void SetOff(bool visble)
{
m_listener.enabled = visble;
}
/// <summary>
/// 音效播放
/// </summary>
public void PlayEffectAudio(string AudioFileName, string audioID, AudioPlayType type, bool isLopp = false)
{
StartPlay(audioID, EffectAudio.transform, type, 0, isLopp);
}
/// <summary>
/// 播放走路特效
/// </summary>
public void PlayMoveAudio(float runTime, string AudioName)
{
if (runTime == 0)
{
return;
}
if (EffectMoveAudio)
{
}
if (!DicAudioClip.ContainsKey(AudioName))
{
if (!ResourceManager.Instance.CacheAssetDic.ContainsKey(AudioName))
{
LogSystem.Log("audio ID:" + AudioName + "is null");
return;
}
else
{
MoveAudio.clip = ResourceManager.Instance.CacheAssetDic[AudioName].AssetObject as AudioClip;
DicAudioClip.Add(AudioName, MoveAudio.clip);
}
}
else
{
MoveAudio.clip = DicAudioClip[AudioName];
}
if (!DicAudioSource.ContainsKey(AudioName))
{
DicAudioSource.Add(AudioName, MoveAudio);
}
MoveAudio.rolloffMode = AudioRolloffMode.Custom;
MoveAudio.maxDistance = 500f;
MoveAudio.loop = true;
MoveAudio.Play();
TimeManager.Instance.Destroy("Run");
TimeManager.Instance.AddTimer("Run", runTime, p =>
{
if (MoveAudio)
{
MoveAudio.Stop();
}
}, MoveAudio);
}
//专门用于场景中多个按钮快速点击的音效重叠
public void PlayOnlyAudio(string AudioFileName, string audioID, AudioPlayType type, bool isLopp = false)
{
CloseAllAudio();
StartPlay(audioID, EffectAudio.transform, type, 0, isLopp);
}
//播放主界面上的音效
public void PlayMainAudio(string audioID, AudioPlayType type = AudioPlayType.PlayeNow, bool isLopp = false)
{
if (string.IsNullOrEmpty(audioID))
{
LogSystem.Log("audio id is null!");
return;
}
if (DicAudioClip.ContainsKey(audioID))
{
PlayEffectAudio("", audioID, type, isLopp);
}
else
{
//主界面是不加载音效的,这里要手动的先加载一下资源
AudioClip audioSou = ResourceManager.Instance.Load("PublicAudio/" + audioID) as AudioClip;
DicAudioClip.Add(audioID, audioSou);
PlayEffectAudio("", audioID, type, isLopp);
}
}
/// <summary>
/// 背景音效播放
/// </summary>
/// <param name="blackAudioName">背景音效名字</param>
/// <param name="IsAsset">是否是AB加载</param>
public void PlayBackAudio(string backAudioName)
{
if (BackAudio != null)
{
m_backSource.clip = ResourceManager.Instance.Load(ResourcesDefine.PublicAudioDefine + backAudioName) as AudioClip;
m_backSource.loop = true;
m_backSource.Play();
m_backSource.volume = 0.5f;
}
}
//开始播放
private AudioSource StartPlay(string audioID, Transform trans, AudioPlayType type, float PlayType, bool isLoop = false)
{
AudioSource[] audios = trans.GetComponents<AudioSource>();
if (CheckSameAudio(audios, audioID, type))
{
return m_audio;
}
for (int i = 0; i < audios.Length; ++i)
{
//播放新的音效
//当前source正在播放
if (audios[i].isPlaying)
{
continue;
}
m_audio = audios[i];
Play(audioID, isLoop);
return m_audio;
}
m_audio = trans.gameObject.AddComponent<AudioSource>();
Play(audioID, isLoop);
return m_audio;
}
private bool CheckSameAudio(AudioSource[] audios, string audioID, AudioPlayType type)
{
for (int i = 0; i < audios.Length; ++i)
{
//检测是否缓存在音效数组中
if (audios[i].clip == null)
{
continue;
}
if (audios[i].clip.name != audioID)
{
continue;
}
if (type == AudioPlayType.PlayeNow)
{
//如果是立即播放类型就立即播放
audios[i].Play();
m_audio = audios[i];
return true;
}
else
{
//如果是等待播放就等待播放完
if (!audios[i].isPlaying)
{
audios[i].Play();
m_audio = audios[i];
return true;
}
}
}
m_audio = null;
return false;
}
/// <summary>
/// 播放音效
/// </summary>
/// <param name="audioID"></param>
/// <param name="isLoop"></param>
private void Play(string audioID, bool isLoop)
{
if (!DicAudioClip.ContainsKey(audioID))
{
if (!ResourceManager.Instance.CacheAssetDic.ContainsKey(audioID))
{
LogSystem.Log("audio ID:" + audioID + "is null");
return;
}
else
{
m_audio.clip = ResourceManager.Instance.CacheAssetDic[audioID].AssetObject as AudioClip;
DicAudioClip.Add(audioID, m_audio.clip);
}
}
else
{
m_audio.clip = DicAudioClip[audioID];
}
if (!DicAudioSource.ContainsKey(audioID))
{
DicAudioSource.Add(audioID, m_audio);
}
m_audio.rolloffMode = AudioRolloffMode.Custom;
m_audio.maxDistance = 500f;
m_audio.Play();
m_audio.loop = isLoop;
}
/// <summary>
/// 关闭音乐
/// </summary>
/// <param name="audioID"></param>
public void CloseAudio(string audioID)
{
if (DicAudioSource.ContainsKey(audioID))
{
DicAudioSource[audioID].Stop();
DicAudioSource[audioID].clip = null;
}
else
{
LogSystem.Log("audio id: " + audioID + "not exist");
}
}
//关闭音乐,可以重复使用
public void CloseAudio2(string audioID)
{
if (DicAudioSource.ContainsKey(audioID))
{
DicAudioSource[audioID].Stop();
DicAudioSource[audioID].clip = null;
//LogSystem.Log(audioID + " : " + DicAudioSource[audioID].isPlaying);
}
else
{
LogSystem.Log("audio id: " + audioID + "not exist");
}
//还得关闭脚步声音
if (MoveAudio && MoveAudio.clip && MoveAudio.clip.name.Equals(audioID) && MoveAudio.isPlaying)
{
MoveAudio.Stop();
//LogSystem.Log(MoveAudio.name + " was stop", LogColor.yellow);
}
}
// 停止所有音效
public void CloseAllAudio()
{
IEnumerator<KeyValuePair<string, AudioSource>> enumerator = DicAudioSource.GetEnumerator();
while (enumerator.MoveNext())
{
//注意,可能为null
if (enumerator.Current.Value != null && enumerator.Current.Value.isPlaying)
{
enumerator.Current.Value.Stop();
}
}
}
//当前某个音效是否正在播放
public bool IsPlaying(string audioName)
{
Dictionary<string, AudioSource> temp = new Dictionary<string, AudioSource>();
IEnumerator<KeyValuePair<string, AudioSource>> enumerator = DicAudioSource.GetEnumerator();
while (enumerator.MoveNext())
{
//注意,可能为null
if (enumerator.Current.Value != null && enumerator.Current.Value.isPlaying)
{
if (enumerator.Current.Key.Equals(audioName))
{
return true;
}
}
}
return false;
}
//获取当前正在播放的音效
public Dictionary<string, AudioSource> GetCurrentPlayingAudio()
{
Dictionary<string, AudioSource> temp = new Dictionary<string, AudioSource>();
IEnumerator<KeyValuePair<string, AudioSource>> enumerator = DicAudioSource.GetEnumerator();
while (enumerator.MoveNext())
{
//注意,可能为null
if (enumerator.Current.Value != null && enumerator.Current.Value.isPlaying)
{
//temp.Clear();
temp.Add(enumerator.Current.Key, enumerator.Current.Value);
}
}
return temp;
}
//获取最近播放的一个语音
public void Clear()
{
DicAudioClip.Clear();
DicAudioSource.Clear();
}
}