227 lines
7.2 KiB
C#
227 lines
7.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
|
|
namespace GeneralTools
|
|
{
|
|
public class AudioManager : SingletonBaseAttribute<AudioManager>
|
|
{
|
|
private AudioSource bgmSource;
|
|
|
|
protected List<AudioSource> playingAudioList = new List<AudioSource>();
|
|
|
|
private Dictionary<string, AudioClip> clipDict = new Dictionary<string, AudioClip>();
|
|
|
|
private string thirdResPath;
|
|
|
|
public override void IStart()
|
|
{
|
|
thirdResPath = DevicePath.StreamingAssetsPath + "/Audio/";
|
|
Init();
|
|
KeyDownManager.Instance.AddKeyDownAction(KeyCode.F4, () => { MuteBGM(); }, "静音/恢复BGM", false, 0);
|
|
}
|
|
public override void IOnApplicationQuit()
|
|
{
|
|
bgmSource.Stop();
|
|
for (int i = 0; i < playingAudioList.Count; i++)
|
|
{
|
|
playingAudioList[i].Stop();
|
|
}
|
|
playingAudioList.Clear();
|
|
}
|
|
void Init()
|
|
{
|
|
//创建BGM组件
|
|
if (gameObject.GetComponent<AudioSource>() == null)
|
|
{
|
|
bgmSource = gameObject.AddComponent<AudioSource>();
|
|
bgmSource.loop = true;
|
|
}
|
|
else
|
|
{
|
|
bgmSource = gameObject.GetComponent<AudioSource>();
|
|
}
|
|
//读取音效 存入词典
|
|
AudioClip[] clip = Resources.LoadAll<AudioClip>("Audio");
|
|
for (int i = 0; i < clip.Length; i++)
|
|
{
|
|
AddClipToDict(clip[i], clip[i].name);
|
|
}
|
|
//加载外部音频
|
|
LoadThirdRes();
|
|
}
|
|
void AddClipToDict(AudioClip clip, string url)
|
|
{
|
|
string clipName = Path.GetFileNameWithoutExtension(Path.GetFullPath(url).Replace(Path.GetFullPath(thirdResPath), ""));
|
|
if (!clipDict.ContainsKey(clipName))
|
|
{
|
|
clipDict[clipName] = clip;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("音频文件名:" + clipName + " 文件名重复");
|
|
}
|
|
}
|
|
public void PlayBGM(AudioClip clip, float volume = 1.0f)
|
|
{
|
|
if (clip == null)
|
|
return;
|
|
if (!ConfigHelper.isAllowPlayBGM)
|
|
{
|
|
Debug.LogError("BGM设置为不播放");
|
|
return;
|
|
}
|
|
bgmSource.Stop();
|
|
bgmSource.clip = clip;
|
|
bgmSource.loop = true;
|
|
SetBGMVolume(volume);
|
|
bgmSource.Play();
|
|
}
|
|
|
|
public void PlayBGM(string clipName, float volume = 1.0f)
|
|
{
|
|
if (!clipDict.ContainsKey(clipName))
|
|
{
|
|
Debug.LogError("未找到音乐名:" + clipName);
|
|
return;
|
|
}
|
|
PlayBGM(clipDict[clipName], volume);
|
|
}
|
|
public void StopBGM()
|
|
{
|
|
bgmSource.Stop();
|
|
}
|
|
public void SetBGMVolume(float volume)
|
|
{
|
|
bgmSource.volume = volume;
|
|
}
|
|
public void MuteBGM()
|
|
{
|
|
bgmSource.mute = !bgmSource.mute;
|
|
}
|
|
public void MuteBGM(bool isMute)
|
|
{
|
|
bgmSource.mute = isMute;
|
|
}
|
|
public void PauseBgm()
|
|
{
|
|
if (bgmSource.clip == null)
|
|
return;
|
|
if (bgmSource.isPlaying)
|
|
{
|
|
bgmSource.Pause();
|
|
}
|
|
else
|
|
{
|
|
bgmSource.Play();
|
|
}
|
|
}
|
|
public void PauseBgm(bool isPause)
|
|
{
|
|
if (isPause)
|
|
{
|
|
bgmSource.Pause();
|
|
}
|
|
else
|
|
{
|
|
bgmSource.Play();
|
|
}
|
|
}
|
|
private void PlayShot(AudioClip clip, float volume = 1.0f)
|
|
{
|
|
if (playingAudioList.Count >= ConfigHelper.audioShotLimit)
|
|
{
|
|
Debug.LogError("同时播放音效数量超出上限");
|
|
return;
|
|
}
|
|
CreateAudio(clip, volume);
|
|
}
|
|
public void PlayShot(string clipName, float volume = 1.0f)
|
|
{
|
|
if (!clipDict.ContainsKey(clipName))
|
|
{
|
|
Debug.LogError("未找到音效名:" + clipName);
|
|
return;
|
|
}
|
|
PlayShot(clipDict[clipName], volume);
|
|
}
|
|
public void PlayShotOnlyOne(string clipName, float volume = 1.0f)
|
|
{
|
|
if (!playingAudioList.Exists(o => o.clip.name == clipName))
|
|
{
|
|
PlayShot(clipName, volume);
|
|
}
|
|
}
|
|
public void PlayShotDelay(string clipName, float delayTime, float volume = 1.0f)
|
|
{
|
|
StartCoroutine(UtilityTool.DelayToInvokeDo(() =>
|
|
{
|
|
PlayShot(clipName, volume);
|
|
}, delayTime));
|
|
}
|
|
private void CreateAudio(AudioClip clip, float volume)
|
|
{
|
|
GameObject gameObj = new GameObject();
|
|
gameObj.name = clip.name;
|
|
gameObj.transform.parent = transform;
|
|
AudioSource newSource = gameObj.AddComponent<AudioSource>();
|
|
newSource.clip = clip;
|
|
newSource.loop = false;
|
|
newSource.volume = volume;
|
|
newSource.Play();
|
|
playingAudioList.Add(newSource);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (playingAudioList.Count <= 0)
|
|
return;
|
|
for (int i = 0; i < playingAudioList.Count; i++)
|
|
{
|
|
if (!playingAudioList[i].isPlaying)
|
|
{
|
|
AudioSource temp = playingAudioList[i];
|
|
playingAudioList.RemoveAt(i);
|
|
#if UNITY_2018_1_OR_NEWER
|
|
Destroy(temp.gameObject);
|
|
#else
|
|
DestroyObject(temp.gameObject);
|
|
#endif
|
|
i--;
|
|
}
|
|
}
|
|
}
|
|
void LoadThirdRes()
|
|
{
|
|
#if !UNITY_2017_1_OR_NEWER
|
|
return;
|
|
#endif
|
|
if (!Directory.Exists(thirdResPath))
|
|
return;
|
|
//读取外部音效,默认StreamAssets/Audio
|
|
// List<string> mp3Flies = FileReader.GetPathAllFilesRoot(outDir, "*.mp3");
|
|
List<string> oggFlies = FileReader.GetPathAllFilesRoot(thirdResPath, "*.ogg");
|
|
List<string> wavFlies = FileReader.GetPathAllFilesRoot(thirdResPath, "*.wav");
|
|
//for (int i = 0; i < mp3Flies.Count; i++)
|
|
//{
|
|
// string savepath = mp3Flies[i].Replace(".mp3", ".wav");
|
|
// if (!File.Exists(savepath)) //MP3 转wav
|
|
// {
|
|
// FileStream stream = File.Open(mp3Flies[i], FileMode.Open);
|
|
// Mp3FileReader reader = new Mp3FileReader(stream);
|
|
// WaveFileWriter.CreateWaveFile(savepath, reader);
|
|
// }
|
|
//}
|
|
for (int i = 0; i < wavFlies.Count; i++)
|
|
{
|
|
WebRequestManager.Instance.GetAudioClip(wavFlies[i], AddClipToDict, AudioType.WAV);
|
|
}
|
|
for (int i = 0; i < oggFlies.Count; i++)
|
|
{
|
|
WebRequestManager.Instance.GetAudioClip(oggFlies[i], AddClipToDict, AudioType.OGGVORBIS);
|
|
}
|
|
}
|
|
}
|
|
} |