using System.Security.Cryptography.X509Certificates;
|
using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
|
public enum AudioEnum
|
{
|
BGM1,
|
BGM2,
|
UI,//UI按键
|
UIDisable,//UI不可点击_错误
|
BuffAppear,//翻书
|
ChooseBuff,//选择buff后
|
End,//游戏结束
|
BossDie,//BOSS死亡
|
AttackTower,//敌人攻击塔
|
DragTower,//拖动宝石时
|
PutTower,//放下宝石
|
FireTAttack,//火攻击
|
WaterTAttack,//水攻击
|
WoodTAttack,//木攻击
|
LightningSkill,//电技能
|
FireSkill,//火技能
|
FinalScore,//结算分数滚动效果
|
BossWaterSkill,//气泡
|
WaterTowerSkill,//冰淇淋技能
|
WoodSkill,//玉米狙击子弹
|
Upgrade,//合成
|
Gold,//金币
|
GuideOpenBox,//新手引导开宝箱
|
OpenTowerPlace,//开启塔位
|
|
}
|
public class AudioSourceManager : MonoBehaviour
|
{
|
private static AudioSourceManager _ins;
|
|
public static AudioSourceManager Ins
|
{
|
get { return _ins; }
|
}
|
|
private void Awake()
|
{
|
_ins = this;
|
|
clipDic = new Dictionary<string, AudioClip>();
|
|
audioSourceList = new List<AudioSource>();
|
for (int i = 0; i < 5; i++)
|
{
|
audioSourceList.Add(gameObject.AddComponent<AudioSource>());
|
}
|
}
|
|
private AudioSource audioSource;
|
private List<AudioSource> audioSourceList = new List<AudioSource>();
|
|
/// <summary>
|
/// 跳过第一个,第一个用来播放背景音 获取一个空的组件
|
/// </summary>
|
/// <returns></returns>
|
private AudioSource GetAudioSource()
|
{
|
for (int i = 1; i < audioSourceList.Count; i++)
|
{
|
if (!audioSourceList[i].isPlaying)
|
{
|
return audioSourceList[i];
|
}
|
}
|
|
AudioSource tmpAudioSource = gameObject.AddComponent<AudioSource>();
|
audioSourceList.Add(tmpAudioSource);
|
return tmpAudioSource;
|
}
|
|
public AudioClip LoadAudioClip(string musicName)
|
{
|
if (clipDic.ContainsKey(musicName))
|
return clipDic[musicName];
|
else
|
{
|
AudioClip au = Resources.Load<AudioClip>("Music/" + musicName);
|
clipDic.Add(musicName, au);
|
return au;
|
}
|
}
|
|
public void Play(AudioEnum en)
|
{
|
// Debug.Log("播放了音效:"+en);
|
switch (en)
|
{
|
case AudioEnum.BGM1:
|
Play("BGM1", false);
|
break;
|
case AudioEnum.BGM2:
|
Play("BGM2", false);
|
break;
|
case AudioEnum.UI:
|
Play("UI按键", true);
|
break;
|
case AudioEnum.UIDisable:
|
Play("不可点击_错误", true);
|
break;
|
case AudioEnum.BuffAppear:
|
Play("翻书", true);
|
break;
|
case AudioEnum.ChooseBuff:
|
StopOneShot("选择buff后", true);
|
//Play("选择buff后", true);
|
break;
|
case AudioEnum.End:
|
Play("游戏结束", true);
|
break;
|
case AudioEnum.BossDie:
|
Play("BOSS死亡", true);
|
break;
|
case AudioEnum.AttackTower:
|
Play("敌人攻击塔", true);
|
break;
|
case AudioEnum.DragTower:
|
Play("拖动宝石时", true);
|
break;
|
case AudioEnum.PutTower:
|
Play("放下宝石", true);
|
break;
|
case AudioEnum.FireTAttack:
|
Play("火攻击", true, 0.5f);
|
break;
|
case AudioEnum.WaterTAttack:
|
Play("水攻击", true, 0.5f);
|
break;
|
case AudioEnum.WoodTAttack:
|
Play("木攻击", true, 0.5f);
|
break;
|
case AudioEnum.LightningSkill:
|
Play("电技能", true);
|
break;
|
case AudioEnum.FireSkill:
|
Play("火焰喷射", true);
|
break;
|
case AudioEnum.FinalScore:
|
Play("结算分数", true);
|
break;
|
case AudioEnum.BossWaterSkill:
|
//Play("气泡", true);
|
PlayBossWaterSkill("气泡");
|
break;
|
case AudioEnum.WaterTowerSkill:
|
Play("冰淇淋技能", true);
|
break;
|
case AudioEnum.WoodSkill:
|
Play("玉米狙击子弹", true);
|
break;
|
case AudioEnum.Upgrade:
|
Play("合成", true);
|
break;
|
case AudioEnum.Gold:
|
Play("金币", true);
|
break;
|
case AudioEnum.GuideOpenBox:
|
Play("新手引导开宝箱", true);
|
break;
|
case AudioEnum.OpenTowerPlace:
|
Play("开启塔位", true);
|
break;
|
|
}
|
}
|
|
|
Dictionary<string, AudioClip> clipDic;
|
|
private void Play(string musicName, bool isOneShot, float volume = 1.0f)
|
{
|
AudioClip au = LoadAudioClip(musicName);
|
AudioSource tmpAudioSource;
|
//tmpAudioSource.volume = PlayerPrefs.GetFloat("BgmVal");
|
|
if (isOneShot)
|
{
|
tmpAudioSource = GetAudioSource();
|
tmpAudioSource.clip = au;
|
tmpAudioSource.loop = false;
|
|
//tmpAudioSource.PlayOneShot(au);//无法立刻暂停
|
StartCoroutine(RemoAudio(tmpAudioSource, au));
|
}
|
else
|
{
|
tmpAudioSource = audioSourceList[0];
|
if (tmpAudioSource.clip == au)
|
{
|
return;
|
}
|
tmpAudioSource.clip = null;
|
tmpAudioSource.clip = au;
|
tmpAudioSource.loop = true;
|
}
|
tmpAudioSource.Play();
|
tmpAudioSource.volume = volume;
|
}
|
|
AudioSource bossWaterSkillAS;
|
/// <summary>
|
/// BOSS气泡技能 38秒钟,所以不用循环播放
|
/// </summary>
|
/// <param name="musicName"></param>
|
/// <param name="volume"></param>
|
private void PlayBossWaterSkill(string musicName, float volume = 1.0f)
|
{
|
Debug.Log("BOSS气泡技能 38秒钟,所以不用循环播放");
|
|
AudioClip au = LoadAudioClip(musicName);
|
bossWaterSkillAS = GetAudioSource();
|
bossWaterSkillAS.clip = au;
|
bossWaterSkillAS.loop = false;
|
|
bossWaterSkillAS.Play();
|
bossWaterSkillAS.volume = volume;
|
}
|
|
/// <summary>
|
/// BOSS气泡技能结束
|
/// </summary>
|
public void StopBossWaterSkill()
|
{
|
Debug.Log("BOSS气泡技能结束");
|
if (bossWaterSkillAS != null)
|
{
|
bossWaterSkillAS.Stop();
|
bossWaterSkillAS.clip = null;
|
if (audioSourceList.Count > 5)
|
{
|
audioSourceList.Remove(bossWaterSkillAS);
|
Destroy(bossWaterSkillAS);
|
bossWaterSkillAS = null;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 暂停背景音乐
|
/// </summary>
|
public void StopBGAudio()
|
{
|
if (audioSourceList[0].isPlaying)
|
{
|
audioSourceList[0].Stop();
|
}
|
}
|
|
/// <summary>
|
/// 继续背景音乐
|
/// </summary>
|
public void RestartBGAudio()
|
{
|
if (!audioSourceList[0].isPlaying)
|
audioSourceList[0].Play();
|
}
|
|
|
/// <summary>
|
/// 销毁音效
|
/// </summary>
|
/// <param name="audioClip"></param>
|
private IEnumerator RemoAudio(AudioSource tmpAudioSource, AudioClip au)
|
{
|
if (audioSourceList.Count > 5)
|
{
|
yield return new WaitForSeconds(au.length);
|
audioSourceList.Remove(tmpAudioSource);
|
Destroy(tmpAudioSource);
|
}
|
yield break;
|
}
|
|
|
/// <summary>
|
/// 调整音效时更新音量
|
/// </summary>
|
public void UpdateAudioVal()
|
{
|
foreach (AudioSource val in audioSourceList)
|
{
|
//便利所有的Bgm ,设置音量
|
if (val)
|
{
|
val.volume = PlayerPrefs.GetFloat("BgmVal");
|
}
|
|
}
|
}
|
|
/// <summary>
|
/// 删除指定音效
|
/// </summary>
|
/// <param name="audioName">被删除的音乐</param>
|
public void DeleteAudio(string audioName)
|
{
|
foreach (AudioSource val in audioSourceList)
|
{
|
if (val && val.clip.name == audioName)
|
{
|
audioSourceList.Remove(val);
|
Destroy(val.gameObject);
|
break;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 音乐暂停
|
/// </summary>
|
/// <param name="isStop">是否暂停</param>
|
public void StopAudio(bool isStop)
|
{
|
foreach (AudioSource val in audioSourceList)
|
{
|
if (isStop && val)
|
{
|
val.Pause();
|
}
|
else if (val)
|
{
|
val.Play();
|
}
|
}
|
}
|
|
/// <summary>
|
/// 判断当前音乐是否存在
|
/// </summary>
|
/// <param name="audioName">音乐名字</param>
|
/// <returns></returns>
|
private bool AudioIsTrue(string audioName)
|
{
|
foreach (AudioSource val in audioSourceList)
|
{
|
if (val && val.clip.name == audioName)
|
{
|
return true;
|
}
|
|
}
|
return false;
|
}
|
|
/// <summary>
|
/// 停止播放某一个音效
|
/// </summary>
|
/// <param name="audioName"></param>
|
private void StopOneShot(string musicName, bool isOneShot)
|
{
|
foreach (AudioSource val in audioSourceList)
|
{
|
if (val && val.clip && val.clip.name == musicName)
|
{
|
val.Stop();
|
val.clip = null;
|
break;
|
}
|
}
|
|
Play(musicName, isOneShot);
|
}
|
|
}
|