音效管理类

Posted huwenya

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了音效管理类相关的知识,希望对你有一定的参考价值。

using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//同时播放一首背景音乐
//同时播放N个音效
//对象池管理Audiosource组件
//背景音乐的声音淡入淡出
//调整背景音乐的音量
//调整音效的音量
//本地缓存声音音量
//切换场景不销毁
//获取播放音效结束回调
//播放3d音效

public class AudioManager : MonoBehaviour {
#region MyRegion
////单例
//public static AudioManager Instance;
//Dictionary<int, string> audioPathDict; // 存放音频文件路径
//AudioSource musicAudioSource;
//List<AudioSource> unusedSoundAudioSourceList; // 存放可以使用的音频组件
//List<AudioSource> usedSoundAudioSourceList; // 存放正在使用的音频组件
//Dictionary<int, AudioClip> audioClipDict; //缓存音频文件
//float musicVolume = 1;
//float soundVolume = 1;
//string musicVolumePrefs = "MusicVolume";
//string soundVolumePrefs = "SoundVolume";
//int poolCount = 3; //对象池数量
//private void Awake()
//{
// DontDestroyOnLoad(this.gameObject);
// Instance = this;

// audioPathDict = new Dictionary<int, string>() //这里设置音频文件路径,需要修改
// {
// { 1,"MAC/Cinematic Impact Logo _Arabic Vocals"},
// { 2,"MAC/Epic Low Hit 02"},
// { 3,"MAC/Epic Low Hit 03"},
// { 4,"MAC/Epic Low Hit 04"}
// };
// musicAudioSource = gameObject.AddComponent<AudioSource>();
// unusedSoundAudioSourceList = new List<AudioSource>();
// usedSoundAudioSourceList = new List<AudioSource>();
// audioClipDict = new Dictionary<int, AudioClip>();
//}
//private void Start()
//{
// //从本地缓存读取声音音量
// if (PlayerPrefs.HasKey(musicVolumePrefs))
// {
// musicVolume = PlayerPrefs.GetFloat(musicVolumePrefs);
// }
// if (PlayerPrefs.HasKey(soundVolumePrefs))
// {
// musicVolume = PlayerPrefs.GetFloat(soundVolumePrefs);
// }
//}
///// <summary>
///// 播放背景音乐
///// </summary>
///// <param name="id"></param>
///// <param name="loop"></param>
//public void PlayMusic(int id, bool loop = true)
//{
// //通过Tween将声音淡入淡出
// DOTween.To(() => musicAudioSource.volume, value => musicAudioSource.volume = value, 0, 0.5f).OnComplete(() =>
// {
// musicAudioSource.clip = GetAudioClip(id);
// musicAudioSource.clip.LoadAudioData(); //加载音频数据
// musicAudioSource.loop = loop;
// musicAudioSource.volume = musicVolume;
// musicAudioSource.Play();
// DOTween.To(() => musicAudioSource.volume, value => musicAudioSource.volume = value, musicVolume, 0.5f);
// });
//}
///// <summary>
///// 播放音效
///// </summary>
///// <param name="id"></param>
///// <param name="action"></param>
//public void PlaySound(int id, Action action = null)
//{
// if (unusedSoundAudioSourceList.Count != 0)
// {
// AudioSource audioSource = UnusedToUsed();
// audioSource.clip = GetAudioClip(id);
// audioSource.clip.LoadAudioData();
// audioSource.Play();
// StartCoroutine(WaitPlayEnd(audioSource, action));
// }
// else
// {
// AddAudioSource();
// AudioSource audioSource = UnusedToUsed();
// audioSource.clip = GetAudioClip(id);
// audioSource.clip.LoadAudioData();
// audioSource.volume = soundVolume;
// audioSource.loop = false;
// audioSource.Play();
// StartCoroutine(WaitPlayEnd(audioSource, action));
// }
//}
///// <summary>
///// 播放3d音效
///// </summary>
///// <param name="id"></param>
///// <param name="position"></param>
//public void Play3dSound(int id, Vector3 position)
//{
// AudioClip ac = GetAudioClip(id);
// AudioSource.PlayClipAtPoint(ac, position);
//}
///// <summary>
///// 当播放音效结束后,将其移至未使用集合
///// </summary>
///// <param name="audioSource"></param>
///// <param name="action"></param>
///// <returns></returns>
//IEnumerator WaitPlayEnd(AudioSource audioSource, Action action)
//{
// yield return new WaitUntil(() => { return !audioSource.isPlaying; });
// UsedToUnused(audioSource);
// if (action != null)
// action();
//}
///// <summary>
///// 获取音频文件,获取后会缓存一份
///// </summary>
///// <param name="id"></param>
///// <returns></returns>
//private AudioClip GetAudioClip(int id)
//{
// if (!audioClipDict.ContainsKey(id))
// {
// if (!audioPathDict.ContainsKey(id)) return null;
// AudioClip ac = Resources.Load(audioPathDict[id]) as AudioClip;
// audioClipDict.Add(id, ac);
// }
// return audioClipDict[id];
//}
///// <summary>
///// 添加音频组件
///// </summary>
///// <returns></returns>
//private AudioSource AddAudioSource()
//{
// if (unusedSoundAudioSourceList.Count != 0)
// {
// return UnusedToUsed();
// }
// else
// {
// AudioSource audioSource = gameObject.AddComponent<AudioSource>();
// unusedSoundAudioSourceList.Add(audioSource);
// return audioSource;
// }
//}
///// <summary>
///// 将未使用的音频组件移至已使用即合理
///// </summary>
///// <returns></returns>
//private AudioSource UnusedToUsed()
//{
// AudioSource audioSource = unusedSoundAudioSourceList[0];
// unusedSoundAudioSourceList.RemoveAt(0);
// usedSoundAudioSourceList.Add(audioSource);
// return audioSource;
//}
///// <summary>
///// 将使用完的音频组件移至未使用集合里
///// </summary>
///// <param name="audioSource"></param>
//private void UsedToUnused(AudioSource audioSource)
//{
// if (usedSoundAudioSourceList.Contains(audioSource))
// {
// usedSoundAudioSourceList.Remove(audioSource);
// }
// if (unusedSoundAudioSourceList.Count >= poolCount)
// {
// Destroy(audioSource);
// }
// else if (audioSource != null && !unusedSoundAudioSourceList.Contains(audioSource))
// {
// unusedSoundAudioSourceList.Add(audioSource);
// }
//}
///// <summary>
///// 修改背景音乐音量
///// </summary>
///// <param name="volume"></param>
//public void ChangeMusicVolume(float volume)
//{
// musicVolume = volume;
// musicAudioSource.volume = volume;
// PlayerPrefs.SetFloat(musicVolumePrefs, volume);
//}
//public void ChangeSoundVolume(float volume)
//{
// soundVolume = volume;
// for (int i = 0; i < unusedSoundAudioSourceList.Count; i++)
// {
// unusedSoundAudioSourceList[i].volume = volume;
// }
// for (int i = 0; i < usedSoundAudioSourceList.Count; i++)
// {
// usedSoundAudioSourceList[i].volume = volume;
// }
// PlayerPrefs.SetFloat(soundVolumePrefs, volume);
//}
#endregion
public static AudioManager Instance;
Dictionary<int, string> musicPathDict = null;
Dictionary<int, AudioClip> soundClipDict = null;
List<AudioSource> unusedAudioSourceList = null;
List<AudioSource> usedAudioSourceList = null;
AudioSource musicAudioSource;
int poolCount = 3;
float musicVolume = 1;
float soundVolume = 1;
string musicPrefs = "MusicPrefs";
string soundPrefs = "SoundPrefs";
private void Awake()
{
DontDestroyOnLoad(gameObject);
Instance = this;
musicPathDict = new Dictionary<int, string>()
{
{ 1,"MAC/Cinematic Impact Logo _Arabic Vocals"},
{ 2,"MAC/Epic Low Hit 01"},
{ 3,"MAC/Epic Low Hit 02"}
};
soundClipDict = new Dictionary<int, AudioClip>();
unusedAudioSourceList = new List<AudioSource>();
usedAudioSourceList = new List<AudioSource>();
musicAudioSource = gameObject.AddComponent<AudioSource>();
}
private void Start()
{
if (PlayerPrefs.HasKey(musicPrefs))
{
musicVolume = PlayerPrefs.GetFloat(musicPrefs);
}
if (PlayerPrefs.HasKey(soundPrefs))
{
soundVolume = PlayerPrefs.GetFloat(soundPrefs);
}
}
public void PlayMusic(int id, bool loop = true)
{
DOTween.To(() => musicAudioSource.volume, value => musicAudioSource.volume = value, 0, 0.5f).OnComplete(() =>
{
musicAudioSource.clip = GetAudioClip(id);
musicAudioSource.clip.LoadAudioData();
musicAudioSource.volume = musicVolume;
musicAudioSource.loop = loop;
musicAudioSource.Play();
DOTween.To(() => musicAudioSource.volume, value => musicAudioSource.volume = value, musicVolume, 0.5f);
});
}
public void PlaySound(int id,Action action = null)
{
if (unusedAudioSourceList.Count!=0)
{
AudioSource audioSource = UnusedToUsed();
audioSource.clip = GetAudioClip(id);
audioSource.clip.LoadAudioData();
audioSource.Play();
StartCoroutine(WaidToEnd(audioSource, action));
}
else
{
AddAudioClip();
AudioSource audioSource = UnusedToUsed();
audioSource.clip = GetAudioClip(id);
audioSource.clip.LoadAudioData();
audioSource.loop = false;
audioSource.volume = soundVolume;
audioSource.Play();
StartCoroutine(WaidToEnd(audioSource, action));
}
}
IEnumerator WaidToEnd(AudioSource audioSource,Action action)
{
yield return new WaitUntil(() => { return !audioSource.isPlaying; });
UsedToUnused(audioSource);
if (action!=null)
{
action();
}
}
private AudioClip GetAudioClip(int id)
{
if (!soundClipDict.ContainsKey(id))
{
if (!musicPathDict.ContainsKey(id)) return null;
AudioClip audioClip = Resources.Load(musicPathDict[id]) as AudioClip;
soundClipDict.Add(id, audioClip);
}
return soundClipDict[id];
}
private AudioSource AddAudioClip()
{
if (unusedAudioSourceList.Count!=0)
{
return UnusedToUsed();
}
else
{
AudioSource audioSource = gameObject.AddComponent<AudioSource>();
unusedAudioSourceList.Add(audioSource);
return audioSource;
}
}
private AudioSource UnusedToUsed()
{
AudioSource audioSource = unusedAudioSourceList[0];
unusedAudioSourceList.RemoveAt(0);
usedAudioSourceList.Add(audioSource);
return audioSource;
}
private void UsedToUnused(AudioSource audioSource)
{
if (usedAudioSourceList.Contains(audioSource))
{
usedAudioSourceList.Remove(audioSource);
}
if (unusedAudioSourceList.Count>=poolCount)
{
Destroy(audioSource);
}
else if (audioSource!=null&&!unusedAudioSourceList.Contains(audioSource))
{
unusedAudioSourceList.Add(audioSource);
}
}
public void ChangeMusicVolume(float volume)
{
musicVolume = volume;
musicAudioSource.volume = volume;
PlayerPrefs.SetFloat(musicPrefs, volume);
}
public void ChangeSoundVolume(float volume)
{
soundVolume = volume;
}
}
























































































































































































































































































































































以上是关于音效管理类的主要内容,如果未能解决你的问题,请参考以下文章

realtek高清晰音频管理器没有音效选项

高清晰音频管理器,怎么突然调解不了音效了?

win10高清晰音频管理器没有音效选项

Unity SKFramework框架Audio音频管理器

我的Realtek高清晰音频管理器的平衡器和环境音效不见了,怎么恢复啊?

Unity 入门笔记 - 05 - 动画事件&类&音效&对话框