unity---音乐模块
音乐模块#
检测音效是否在播放,没有播放就Destroy#
完整代码#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class MusicMgr : Singleton<MusicMgr>
{
private AudioSource bgMusic = null;
private float bgValue=1;
private GameObject soundObj= null;
private List<AudioSource> soundList=new List<AudioSource>();
private float soundValue=1f;
public void PlayBgMusic(string name){
if(bgMusic==null){
GameObject obj = new GameObject();
obj.name="bgMusic";
bgMusic=obj.AddComponent<AudioSource>();
}
ResMgr.Instance.LoadAsync<AudioClip>("Music/Bgm/"+name,(clip)=>{
bgMusic.clip=clip;
bgMusic.loop=true;
bgMusic.volume=bgValue;
bgMusic.Play();
});
}
public void PauseBGMusic(){
if(bgMusic==null){
return ;
}
bgMusic.Pause();
}
public void ChangeBGMusic(float v){
bgValue = v;
if(bgMusic==null){
return ;
}
bgMusic.volume=bgValue;
}
public void StopBgMusic(){
if(bgMusic==null){
return ;
}
bgMusic.Stop();
}
public void PlaySound(string name,bool isLoop,UnityAction<AudioSource> callBack=null){
if(soundObj==null){
soundObj=new GameObject();
soundObj.name="Sound";
}
ResMgr.Instance.LoadAsync<AudioClip>("Music/Sound/"+name,(clip)=>{
AudioSource source =soundObj.AddComponent<AudioSource>();
source.clip=clip;
source.loop=isLoop;
source.volume=soundValue;
source.Play();
soundList.Add(source);
if(callBack!=null){
callBack(source);
}
});
}
public void StopSound(AudioSource source){
if(soundList.Contains(source)){
soundList.Remove(source);
source.Stop();
GameObject.Destroy(source);
}
}
public void ChangeSoundValue(float value){
soundValue=value;
for(int i=0;i<soundList.Count;i++){
soundList[i].volume=value ;
}
}
public MusicMgr(){
MonoMgr.Instance.AddUpdateListener(Update);
}
private void Update(){
for(int i=soundList.Count-1;i>0;--i){
if(!soundList[i].isPlaying){
GameObject.Destroy(soundList[i]);
soundList.RemoveAt(i);
}
}
}
}
测试代码#
自动生成两个按钮,分别为播放和停止,播放相应的bgm和音效;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MusicTest : MonoBehaviour
{
// Start is called before the first frame update
private AudioSource source;
private void OnGUI() {
if(GUI.Button(new Rect(0,0,100,100),"播放音效")){
//播放音效
MusicMgr.Instance.PlaySound("kaca",false,(s)=>{
source=s;
});
//播放背景音乐
MusicMgr.Instance.PlayBgMusic("swrm");
}
if(GUI.Button(new Rect(0,100,100,100),"停止音效")){
MusicMgr.Instance.StopSound(source);
MusicMgr.Instance.StopBgMusic();
source=null;
}
}
}
音频API总结#
在Unity中,可以通过以下方法来控制AudioSource:
-
Play():开始播放音频。可以设置播放的起始时间。
-
Stop():停止播放音频。
-
Pause():暂停正在播放的音频。
-
UnPause():取消暂停并继续播放音频。
-
SetLoop(bool loop):设置音频是否循环播放。
-
SetVolume(float volume):设置音频的音量,范围为0.0到1.0。
-
SetPitch(float pitch):设置音频的音调,范围为0.5到2.0。
-
SetSpatialBlend(float blend):设置音频的空间混合,控制音频在3D空间中的定位。如果设为0,则音频是2D音效,如果设为1,则音频是3D音效。
-
SetSpatializerFloat(int index, float value):设置音频的空间混合器的浮点类型参数。
-
SetSpatializerInt(int index, int value):设置音频的空间混合器的整数类型参数。
-
SetSpatializerBool(int index, bool value):设置音频的空间混合器的布尔类型参数。
-
GetSpectrumData(float[] samples, int channel, FFTWindow window):获取音频频谱数据,用于实现音乐可视化效果。
另外,还可以通过AudioSource组件的属性来进一步控制音频的播放,如clip属性可以设置要播放的音频剪辑,outputAudioMixerGroup属性可以设置音频输出到的混音器组等。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示