首页 > 其他 > 详细

音效管理类

时间:2018-06-21 01:00:59      阅读:49      评论:0      收藏:0      [点我收藏+]

标签:string   ner   destroy   loop   ati   urn   集合   ddc   object   

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;
}
}

音效管理类

标签:string   ner   destroy   loop   ati   urn   集合   ddc   object   

原文:https://www.cnblogs.com/huwenya/p/9206755.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号