Unity制作一个简易的音频管理器

不多 bb,直接上代码

AudioManger.cs

点击展开代码 >folded
using System;
using UnityEngine;
using UnityEngine.Audio;

public class AudioManger : MonoBehaviour
{
public static AudioManger Instance;
public AudioMixer audioMixer;

//声明音频与音频源
[Header("音频源")]
public Sound[] BGMSound, EffectSound, VoiceSound;

public AudioSource BGMSource, EffectSource, VoiceSource;

private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}

public void PlayBGM(string name, bool loop)
{
Sound s = Array.Find(BGMSound, x => x.name == name);
if (s == null)
{
Debug.LogWarning("并未找到任何对应的音频");
}
else
{
BGMSource.clip = s.clip;
BGMSource.loop = loop;
BGMSource.Play();
}
}

public void PlayVoice(string name, bool loop)
{
Sound s = Array.Find(VoiceSound, x => x.name == name);
if (s == null)
{
Debug.LogWarning("并未找到任何对应的音频");
}
else
{
VoiceSource.clip = s.clip;
VoiceSource.loop = loop;
VoiceSource.Play();
}
}

public void PlayEffect(string name, bool loop)
{
Sound s = Array.Find(EffectSound, x => x.name == name);
if (s == null)
{
Debug.LogWarning("并未找到任何对应的音频");
}
else
{
EffectSource.clip = s.clip;
EffectSource.loop = loop;
EffectSource.Play();
}
}

public void SetVolume(string Group, float volume) //输入音频组名与音量
{
switch (Group) //控制音频音量,SetVolume(Group,音量)即可更改,方便UI挂载
{
case "MainVolume":
SetMainVolume(volume);
break;

case "BGMVolume":
SetBGMVolume(volume);
break;

case "VoiceVolume":
SetVoiceVolume(volume);
break;

case "SoundEffectVolume":
SetSoundEffectVolume(volume);
break;
}
}

//控制各个分类的音量
private void SetMainVolume(float volume)
{
if (volume == -20f)
{
volume = -80f;
}
audioMixer.SetFloat("MainVolume", volume);
}

private void SetBGMVolume(float volume)
{
if (volume == -20f)
{
volume = -80f;
}
audioMixer.SetFloat("BGMVolume", volume);
}

private void SetVoiceVolume(float volume)
{
if (volume == -20f)
{
volume = -80f;
}
audioMixer.SetFloat("VoiceVolume", volume);
}

private void SetSoundEffectVolume(float volume)
{
if (volume == -20f)
{
volume = -80f;
}
audioMixer.SetFloat("SoundEffectVolume", volume);
}
}

Sound.cs

using UnityEngine;

[System.Serializable]
public class Sound
{
public string name;
public AudioClip clip;
}

如何使用?

将AudioManger.cs挂到某个组件中

或者复制这里的代码重命名为 xxx.prefab

点击展开代码
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &1408547801639375235
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 6543838070776806765}
- component: {fileID: 1043931412460261651}
m_Layer: 0
m_Name: Voice
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &6543838070776806765
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1408547801639375235}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 8921201902579823490}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!82 &1043931412460261651
AudioSource:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1408547801639375235}
m_Enabled: 1
serializedVersion: 4
OutputAudioMixerGroup: {fileID: -6845084537208628627, guid: 6dd93ff7ba820fb43b61225b9c78d232, type: 2}
m_audioClip: {fileID: 8300000, guid: caeb8a434a7b54b488ce406bbfe6f9e3, type: 3}
m_PlayOnAwake: 1
m_Volume: 1
m_Pitch: 1
Loop: 0
Mute: 0
Spatialize: 0
SpatializePostEffects: 0
Priority: 128
DopplerLevel: 1
MinDistance: 1
MaxDistance: 500
Pan2D: 0
rolloffMode: 0
BypassEffects: 0
BypassListenerEffects: 0
BypassReverbZones: 0
rolloffCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
- serializedVersion: 3
time: 1
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
panLevelCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
spreadCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
reverbZoneMixCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
--- !u!1 &2490922878746870056
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 8929473666069690554}
- component: {fileID: 558416099772200229}
m_Layer: 0
m_Name: BGM
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &8929473666069690554
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2490922878746870056}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 8921201902579823490}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!82 &558416099772200229
AudioSource:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2490922878746870056}
m_Enabled: 1
serializedVersion: 4
OutputAudioMixerGroup: {fileID: -6845084537208628627, guid: 6dd93ff7ba820fb43b61225b9c78d232, type: 2}
m_audioClip: {fileID: 8300000, guid: caeb8a434a7b54b488ce406bbfe6f9e3, type: 3}
m_PlayOnAwake: 1
m_Volume: 1
m_Pitch: 1
Loop: 0
Mute: 0
Spatialize: 0
SpatializePostEffects: 0
Priority: 128
DopplerLevel: 1
MinDistance: 1
MaxDistance: 500
Pan2D: 0
rolloffMode: 0
BypassEffects: 0
BypassListenerEffects: 0
BypassReverbZones: 0
rolloffCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
- serializedVersion: 3
time: 1
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
panLevelCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
spreadCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
reverbZoneMixCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
--- !u!1 &3389746942981907501
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 8921201902579823490}
- component: {fileID: 1297022102862039859}
m_Layer: 0
m_Name: MainAudioSystem
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &8921201902579823490
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3389746942981907501}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 8929473666069690554}
- {fileID: 252995412814922973}
- {fileID: 6543838070776806765}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1297022102862039859
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3389746942981907501}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a1b09dd7e9e58d34483728279ca7e9b3, type: 3}
m_Name:
m_EditorClassIdentifier:
audioMixer: {fileID: 24100000, guid: d0853721ddf870944b1e86161e834504, type: 2}
BGMSound:
- name: titleBGM
clip: {fileID: 8300000, guid: f1edf858dcfcd2b4d84c98b1216e0e96, type: 3}
- name: endBGM
clip: {fileID: 8300000, guid: 938ed8e10efb2484f8c13bf6409cbe2e, type: 3}
EffectSound:
- name: UIEnter
clip: {fileID: 8300000, guid: f67770616d7eb23418cb7dc0a019d8a9, type: 3}
VoiceSound: []
BGMSource: {fileID: 558416099772200229}
EffectSource: {fileID: 9028692089391810268}
VoiceSource: {fileID: 1043931412460261651}
--- !u!1 &6074496135704850698
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 252995412814922973}
- component: {fileID: 9028692089391810268}
m_Layer: 0
m_Name: Effect
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &252995412814922973
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6074496135704850698}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 8921201902579823490}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!82 &9028692089391810268
AudioSource:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6074496135704850698}
m_Enabled: 1
serializedVersion: 4
OutputAudioMixerGroup: {fileID: -6845084537208628627, guid: 6dd93ff7ba820fb43b61225b9c78d232, type: 2}
m_audioClip: {fileID: 8300000, guid: caeb8a434a7b54b488ce406bbfe6f9e3, type: 3}
m_PlayOnAwake: 1
m_Volume: 1
m_Pitch: 1
Loop: 0
Mute: 0
Spatialize: 0
SpatializePostEffects: 0
Priority: 128
DopplerLevel: 1
MinDistance: 1
MaxDistance: 500
Pan2D: 0
rolloffMode: 0
BypassEffects: 0
BypassListenerEffects: 0
BypassReverbZones: 0
rolloffCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
- serializedVersion: 3
time: 1
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
panLevelCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
spreadCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
reverbZoneMixCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4

然后挂载到场景中,脚本使用下面的方法即可调用

使用方法

在 AudioManger中,我们提供了三种控制,控制 播放的音频 ,控制 音频的停止 ,与控制音频的音量

函数简介
PlayBGM(string name, bool loop)控制 BGM 的播放与循环
PlayVoice(string name, bool loop)控制 Voice 的播放与循环
PlayEffect(string name, bool loop)控制 Effect 的播放与循环
SetVolume(string Group, float volume)控制各个属性的音量
BGMSource直接控制 BGM 的 Source
EffectSource直接控制 Effect 的 Source
VoiceSource直接控制 Voice 的 Source

控制播放的音频

AudioManger.Instance.PlayBGM("title",true);

其中,PlayBGM为分类

我们暂时先提供了三种类别的音频控制: PlayBGM,PlayVoice,PlayEffect

这三个函数共有以下参数

参数简介值属性可用值
name从管理器中寻找与之相对应名称的音频string依靠脚本
loop是否循环播放boolfalse/true

控制音频的音量

AudioManger.Instance.SetVolume(MainVolume, 20f);

其中,BGM为分类

我们提供了以下参数

参数简介值属性可用值
Group控制指定组stringMainVolume/BGMVolume/VoiceVolume/SoundEffectVolume
volume音量调整float任何 float(理论)