共计 3037 个字符,预计需要花费 8 分钟才能阅读完成。
一:目标
游戏音频的播放在任何游戏中都占据十分重要的位置,音频的播放能够分为两种,一种为游戏音乐,另一种为游戏音效。前者实用于较长的音乐,如游戏背景音乐。第二种实用于比拟短的游戏音乐,如开枪霎时的音效
在 Unity 中有两个对于音频的重要组件:AudioListener 和 AudioSource,通过 AudioListener 进行监听,AudioSource 进行播放
所以咱们须要一个管理器对立治理音频的相干操作
二:解决的问题及长处
——音效的播放、暂停、持续、静音
——背景音乐的播放、暂停、持续、静音、渐隐
三:应用办法
——将所有音频文件对立放在 Resources 文件夹下的 Sounds 文件夹下
——应用 SoundMgr.Ins.XXX
SoundMgr.Ins.PlayBGM("bgm");
SoundMgr.Ins.PlaySound("coin");
SoundMgr.Ins.StopPlay();
SoundMgr.Ins.PausePlay();
SoundMgr.Ins.BGMFade(2);
四:代码实现
using UnityEngine;
/// <summary>
/// 音频管理器
/// </summary>
public class SoundMgr : MonoSingleton<SoundMgr>
{
public const string SoundDir = "Sounds/";// 音频目录
//BGM 播放器
private AudioSource bgmPlayer;
public AudioSource BgmPlayer {get { return bgmPlayer;} }
// 音效播放器
private AudioSource soundPlayer;
public AudioSource SoundPlayer {get { return soundPlayer;} }
// 是否静音
private bool isMute;
public bool IsMute {get { return isMute;} }
private bool bgmFade;// 背景音乐是否渐隐
private float bgmFadeBeginVolume;// 背景音乐渐隐开始时的音量
private float bgmFadeBeginTime;// 背景音乐渐隐开始的工夫
private float bgmFadeDuration;// 背景音乐渐隐的工夫
private void Awake()
{if (bgmPlayer == null)
{bgmPlayer = gameObject.AddComponent<AudioSource>();
bgmPlayer.loop = true;
bgmPlayer.playOnAwake = false;
bgmPlayer.mute = isMute;
}
if (soundPlayer == null)
{soundPlayer = gameObject.AddComponent<AudioSource>();
soundPlayer.loop = false;
soundPlayer.playOnAwake = false;
soundPlayer.mute = isMute;
}
}
/// <summary>
/// 播放音效
/// </summary>
public void PlaySound(string soundName, float volume = 1, bool loop = false)
{AudioClip clip = Resources.Load<AudioClip>(SoundDir + soundName);
if (clip == null)
{Debug.LogError("没有此音频:" + soundName);
return;
}
soundPlayer.clip = clip;
soundPlayer.volume = volume;
soundPlayer.loop = loop;
soundPlayer.Play();}
/// <summary>
/// 播放背景音乐
/// </summary>
public void PlayBGM(string bgmName, float volume = 1, bool loop = true)
{AudioClip clip = Resources.Load<AudioClip>(SoundDir + bgmName);
if (clip == null)
{Debug.LogError("没有此音频:" + bgmName);
return;
}
bgmPlayer.clip = clip;
bgmPlayer.volume = volume;
bgmPlayer.loop = loop;
bgmPlayer.Play();}
/// <summary>
/// 背景音乐渐隐
/// </summary>
public void FadeBGM(float fadeDuration)
{
bgmFade = true;
bgmFadeBeginTime = Time.realtimeSinceStartup;
bgmFadeBeginVolume = bgmPlayer.volume;
bgmFadeDuration = fadeDuration;
}
/// <summary>
/// 暂停播放
/// </summary>
public void PausePlay()
{bgmPlayer.Pause();
soundPlayer.Pause();}
/// <summary>
/// 持续播放
/// </summary>
public void ResumePlay()
{bgmPlayer.Play();
soundPlayer.Play();}
/// <summary>
/// 进行播放
/// </summary>
public void StopPlay()
{bgmPlayer.Stop();
soundPlayer.Stop();}
/// <summary>
/// 设置静音
/// </summary>
public void SetMute(bool b)
{
isMute = b;
bgmPlayer.mute = isMute;
soundPlayer.mute = isMute;
}
/// <summary>
/// 进行播放 BGM
/// </summary>
public void StopPlayBGM()
{bgmPlayer.Stop();
}
/// <summary>
/// 暂停播放 BGM
/// </summary>
public void PausePlayBGM()
{bgmPlayer.Pause();
}
/// <summary>
/// 持续播放 BGM
/// </summary>
public void ResumePlayBGM()
{bgmPlayer.Play();
}
/// <summary>
/// 进行播放音效
/// </summary>
public void StopPlaySound()
{soundPlayer.Stop();
}
private void Update()
{if (bgmFade)
{
float delta = Time.realtimeSinceStartup - bgmFadeBeginTime;
if (delta <= bgmFadeDuration)
{bgmPlayer.volume = Mathf.Lerp(bgmFadeBeginVolume, 0, delta / bgmFadeDuration);
}
else
{
bgmFade = false;
bgmPlayer.volume = 0;
}
}
}
}
图片起源:http://www.coubai.com/ 页游
正文完