using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace EduCoderTool
{
/*
* @func 音效播放
* @author lz
* @data 2020/06/19
*
*/
public class AudioController : MonoBehaviour
{
private static AudioController instance = null;
///
/// 单例
///
public static AudioController Singleton
{
get
{
if (instance == null)
{
instance = GameObject.FindObjectOfType();
if (instance == null)
{
GameObject go = new GameObject("AudioController");
instance = go.AddComponent();
}
}
return instance;
}
}
private const int MaxAudioCount = 10;
private const string ResourcePath = "Audio/";
private const string StreamingAssetsPath = "";
private AudioSource BGMAudioSource;
private AudioSource LastAudioSource;
private Dictionary exitAudioClipResDic = new Dictionary();
#region Mono Function
private void Update()
{
if (Input.GetKeyDown(KeyCode.J))
{
Singleton.SoundPlay("S_XuLi", 1.0f, false, true);
}
if (Input.GetKeyDown(KeyCode.P))
{
SoundStop("S_XuLi", false);
}
}
#endregion
///
/// 播放
///
///
public void SoundPlay(string audioname, float volume = 1, bool onlyOnce = false, bool isLoop = false)
{
if (exitAudioClipResDic.ContainsKey(audioname))
{
AudioClip sound;
exitAudioClipResDic.TryGetValue(audioname, out sound);
if (onlyOnce)
{
AudioSource[] exitSoundArr = this.transform.GetComponentsInChildren();
if (exitSoundArr.Length > 0)
{
for (int i = 0; i < exitSoundArr.Length; i++)
{
if (exitSoundArr[i].gameObject.name == audioname)
return;
}
}
if (ChectIsPlayAudio(audioname))
{
return;
}
else
{
if (sound != null) this.PlayClip(sound, volume, audioname, isLoop);
}
}
else
{
if (sound != null) this.PlayClip(sound, volume, audioname, isLoop);
}
}
else
{
AudioClip sound = this.GetAudioClip(audioname);
if (sound != null)
{
exitAudioClipResDic.Add(audioname, sound);
this.PlayClip(sound, volume, audioname, isLoop);
}
}
}
///
/// 暂停
///
///
public void SoundPause(string audioname)
{
if (this.LastAudioSource != null)
{
this.LastAudioSource.Pause();
}
}
///
/// 暂停所有音效音乐
///
public void SoundAllPause()
{
AudioSource[] allsource = FindObjectsOfType();
if (allsource != null && allsource.Length > 0)
{
for (int i = 0; i < allsource.Length; i++)
{
allsource[i].Pause();
}
}
}
///
/// 停止特定的音效
///
///
public void SoundStop(string audioname, bool isAll = true)
{
if (this.transform != null)
{
AudioSource[] gameObjects = this.transform.GetComponentsInChildren();
foreach (var item in gameObjects)
{
if (item.gameObject.name == audioname)
{
Destroy(item.gameObject);
if (!isAll)
break;
}
}
}
}
///
/// 设置音量
///
public void BGMSetVolume(float volume)
{
if (this.BGMAudioSource != null)
{
this.BGMAudioSource.volume = volume;
}
}
///
/// 播放背景音乐
///
///
///
public void BGMPlay(string bgmname, float volume = 1f)
{
BGMStop();
if (bgmname != null)
{
AudioClip bgmsound = this.GetAudioClip(bgmname);
if (bgmsound != null)
{
this.PlayLoopBGMAudioClip(bgmsound, volume, bgmname);
}
}
}
///
/// 暂停背景音乐
///
public void BGMPause()
{
if (this.BGMAudioSource != null)
{
this.BGMAudioSource.Pause();
}
}
///
/// 停止背景音乐
///
public void BGMStop()
{
if (this.BGMAudioSource != null && this.BGMAudioSource.gameObject)
{
Destroy(this.BGMAudioSource.gameObject);
this.BGMAudioSource = null;
}
}
///
/// 重新播放
///
public void BGMReplay()
{
if (this.BGMAudioSource != null)
{
this.BGMAudioSource.Play();
}
}
bool ChectIsPlayAudio(string audioName)
{
AudioSource[] gameObjects = this.transform.GetComponentsInChildren();
foreach (var item in gameObjects)
{
if (item.gameObject.name == audioName)
{
return true;
}
}
return false;
}
#region 音效资源路径
enum eResType
{
AB = 0,
CLIP = 1
}
///
/// 下载音效
///
///
///
///
private AudioClip GetAudioClip(string aduioname, eResType type = eResType.CLIP)
{
AudioClip audioclip = null;
switch (type)
{
case eResType.AB:
break;
case eResType.CLIP:
audioclip = GetResAudioClip(aduioname);
break;
default:
break;
}
return audioclip;
}
private IEnumerator GetAbAudioClip(string aduioname)
{
yield return null;
}
private AudioClip GetResAudioClip(string aduioname)
{
string clipPath = ResourcePath + aduioname;
AudioClip tempClip = Resources.Load(clipPath) as AudioClip;
if (tempClip == null)
{
Debug.LogFormat("{0} 音频文件查找失败!请检查资源命名或资源是否存在", clipPath);
}
else
{
Debug.LogFormat("{0} 音频加载成功!!!", clipPath);
}
return tempClip;
}
#endregion
#region 背景音乐
///
/// 背景音乐控制器
///
///
///
///
///
private void PlayBGMAudioClip(AudioClip audioClip, float volume = 1f, bool isloop = false, string name = null)
{
if (audioClip == null)
{
return;
}
else
{
GameObject obj = new GameObject(name);
obj.transform.parent = this.transform;
AudioSource LoopClip = obj.AddComponent();
LoopClip.clip = audioClip;
LoopClip.volume = volume;
LoopClip.loop = true;
LoopClip.pitch = 1f;
LoopClip.Play();
this.BGMAudioSource = LoopClip;
}
}
///
/// 播放一次的背景音乐
///
///
///
///
private void PlayOnceBGMAudioClip(AudioClip audioClip, float volume = 1f, string name = null)
{
PlayBGMAudioClip(audioClip, volume, false, name == null ? "BGMSound" : name);
}
///
/// 循环播放的背景音乐
///
///
///
///
private void PlayLoopBGMAudioClip(AudioClip audioClip, float volume = 1f, string name = null)
{
PlayBGMAudioClip(audioClip, volume, true, name == null ? "LoopSound" : name);
}
#endregion
#region 音效
///
/// 播放音效
///
///
///
///
private void PlayClip(AudioClip audioClip, float volume = 1f, string name = null, bool isLoop = false)
{
if (audioClip == null)
{
return;
}
else
{
GameObject obj = new GameObject(name == null ? "SoundClip" : name);
obj.transform.parent = this.transform;
AudioSource source = obj.AddComponent();
if (isLoop == false)
{
StartCoroutine(this.PlayClipEndDestroy(audioClip, obj));
}
source.pitch = 1f;
source.volume = volume;
source.clip = audioClip;
source.loop = isLoop;
source.Play();
this.LastAudioSource = source;
}
}
///
/// 播放玩音效删除物体
///
///
///
///
private IEnumerator PlayClipEndDestroy(AudioClip audioclip, GameObject soundobj)
{
if (soundobj == null || audioclip == null)
{
yield break;
}
else
{
yield return new WaitForSeconds(audioclip.length * Time.timeScale);
Destroy(soundobj);
}
}
#endregion
}
}