using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LuaInterface;
using GameCore;
namespace GameLogic
{
///
/// 扩展音源
///
public class ExtAudioClip {
///
/// 音效名
///
public string name {
get {
if (audioClip == null) return string.Empty;
return audioClip.name;
}
}
///
/// 音效资源
///
public AudioClip audioClip;
///
/// 初始化
///
///
public void Init(AudioClip audioClip) {
this.audioClip = audioClip;
}
///
/// 卸载
///
public void UnLoad() {
if (audioClip == null) return;
App.ResMgr.UnLoadAsset(audioClip.name);
}
}
///
/// 扩展音乐播放器
///
public class ExtAudioSource : MonoBehaviour
{
///
/// 音乐播放源
///
AudioSource audioSource;
public ExtAudioClip clip{get;set;}
public bool isPlaying {
get {
return audioSource.isPlaying;
}
}
public float volume {
get
{
return audioSource.volume;
}
set {
audioSource.volume = value;
}
}
public bool loop {
get
{
return audioSource.loop;
}
set
{
audioSource.loop = value;
}
}
private void Awake()
{
audioSource = this.gameObject.AddComponent();
}
public void Play() {
audioSource.Play();
}
///
/// 播放
///
///
///
public void Play(ExtAudioClip clip)
{
this.clip = clip;
audioSource.clip = clip.audioClip;
audioSource.Play();
}
public void PlayOneShot(ExtAudioClip clip) {
this.clip = clip;
audioSource.clip = clip.audioClip;
audioSource.PlayOneShot(clip.audioClip);
}
///
/// 停止播放
///
public void Stop()
{
audioSource.Stop();
}
}
public class SoundManager : UnitySingleton
{
GameCore.ObjectPool audioClipPool = new GameCore.ObjectPool(null, (toRealease) =>
{
toRealease.UnLoad();
});
///
/// 音乐开关
///
const string IsPlayMusicKey = "IsPlayMusicKey";
///
/// 音效开关
///
const string IsPlayAudioKey = "IsPlayAudioKey";
///
/// 音乐音量
///
const string MusicVolumeKey = "MusicVolumeKey";
///
/// 音效音量
///
const string AudioVolumeKey = "AudioVolumeKey";
///
/// 同时播放的音效数量
///
private int aduioCount = 5;
///
/// 背景音量
///
private float musicVolume = 1f;
///
/// 音效音量
///
private float aduioVolume = 1f;
///
/// 是否关闭声音
///
private bool isPlayMusic = true;
///
/// 是否关闭音效
///
private bool isPlayAudio = true;
///
/// 背景音
///
ExtAudioSource audioSource;
///
/// 音效音
///
ExtAudioSource[] audioSources;
///
/// 下一个背景音
///
ExtAudioClip nextBGM;
///
/// 初始化
///
private void Awake()
{
Init();
gameObject.AddComponent();
audioSources = new ExtAudioSource[aduioCount];
for (int i = 0; i < aduioCount; i++)
{
audioSources[i] = gameObject.AddComponent();
}
audioSource = gameObject.AddComponent();
audioSource.volume = 0f;
audioSource.loop = true;
nextBGM = null;
}
///
/// 初始化
///
private void Init()
{
isPlayMusic = PlayerPrefs.GetInt(IsPlayMusicKey, 1) == 1;
isPlayAudio = PlayerPrefs.GetInt(IsPlayAudioKey, 1) == 1;
}
private void Update()
{
if (nextBGM != null && isPlayMusic)
{
if (audioSource.clip == null || audioSource.volume <= 0)
{
if (audioSource.clip != null)
{
audioClipPool.Release(audioSource.clip);
}
audioSource.Play(nextBGM);
nextBGM = null;
}
else
{
audioSource.volume -= 0.02f;
}
}
else if (audioSource.clip != null && audioSource.volume < musicVolume && isPlayMusic)
{
if (!audioSource.isPlaying) audioSource.Play();
float volume = musicVolume - audioSource.volume;
audioSource.volume += volume > 0.02f ? 0.02f : volume;
}
else if (!isPlayMusic && audioSource.volume > 0)
{
audioSource.volume -= 0.02f;
if (audioSource.volume <= 0) audioSource.Stop();
}
}
///
/// 播放背景音乐
///
public void PlayMusic(string name)
{
try {
if (string.IsNullOrEmpty(name))
return;
if (audioSource.clip != null && name == audioSource.clip.name)
return;
App.ResMgr.LoadAssetAsync(name, (tmpName, clip) =>
{
if (clip == null || (nextBGM!=null && clip == nextBGM.audioClip))
{
App.ResMgr.UnLoadAsset(name);
return;
}
nextBGM = GetClip(clip);
});
}
catch(System.Exception e){
Debug.LogErrorFormat("PlayMusic error:{0}",e.ToString());
}
}
///
/// 播放音效
///
/// Name.
public void PlayAudio(string name)
{
if (string.IsNullOrEmpty(name))
return;
if (!isPlayAudio) return;
App.ResMgr.LoadAssetAsync(name,(tmpName,clip) =>
{
if (clip == null)
{
App.ResMgr.UnLoadAsset(name);
return;
}
ExtAudioSource source = GetAudioSoure();
if (source.isPlaying) source.Stop();
source.volume = AduioVolume;
source.PlayOneShot(GetClip(clip));
});
}
///
/// 获取一个audioClip
///
///
///
private ExtAudioClip GetClip(AudioClip audioClip) {
ExtAudioClip extAudioClip = audioClipPool.Get();
extAudioClip.Init(audioClip);
return extAudioClip;
}
///
/// 回收一个audioClip
///
///
private void ReleaseClip(ExtAudioClip extAudioClip) {
if (extAudioClip == null) return;
audioClipPool.Release(extAudioClip);
}
///
/// 获取最后一个音效播放器
///
/// The audio soure.
private ExtAudioSource GetAudioSoure()
{
ExtAudioSource source = null;
for (int i = 0; i < audioSources.Length; i++)
{
if (!audioSources[i].isPlaying)
source = audioSources[i];
}
source = source == null ? audioSources[0] : source;
ReleaseClip(source.clip);
return source;
}
///
/// 停止所有音效
///
public void StopAllAudio()
{
if (audioSources != null)
{
for (int i = 0; i < audioSources.Length; i++)
{
if (audioSources[i] != null)
{
audioSources[i].Stop();
ReleaseClip(audioSources[i].clip);
}
}
}
}
public float MusicVolume
{
get {
return musicVolume;
}
set {
musicVolume = Mathf.Clamp01(value);
PlayerPrefs.SetFloat(MusicVolumeKey, musicVolume);
}
}
public float AduioVolume
{
get
{
return aduioVolume;
}
set
{
aduioVolume = Mathf.Clamp01(value);
PlayerPrefs.SetFloat(AudioVolumeKey, aduioVolume);
}
}
///
/// 是否播放音乐
///
public bool IsPlayMusic
{
get
{
return isPlayMusic;
}
set
{
isPlayMusic = value;
PlayerPrefs.SetInt(IsPlayMusicKey, isPlayMusic ? 1 : 0);
}
}
///
/// 是否播放音效
///
public bool IsPlayAudio
{
get
{
return isPlayAudio;
}
set
{
isPlayAudio = value;
PlayerPrefs.SetInt(IsPlayAudioKey, isPlayAudio ? 1 : 0);
if (!isPlayAudio)
StopAllAudio();
}
}
}
}