1.首先他是个什么东西
-
他的创建方式Project->右击创建->AudioMixer组件,这个东西只能和AudioSource结合使用,大概就是AudioSource通过混音器输出声音,可以对声音进行更细腻的控制。双击打开大概就如下图所示,标红的地方关键点
-
那下图是我做好的一个混合器,乱七八糟的箭头是啥意思那,
- Gourps-这个就是相当于几个音响,音乐(AudioSource【OutPut】)从那个音响输出,对应关系自己看一下就可以
- Add-同的音响的音效中间这个面板的块上有个Add按钮可以添加不同音效,让声音听起来酷,这里不是专业的只是试了一下。
- Exposed Paramters-这个东西就是一个接口相当于音响的控制按钮,通过脚本去控制这个值,比如上面的三个Volume就是对应三个音响的音量。在那几个圈圈上右击选择带有exposed的可以在Exposed Paramters看到了
- 其实还是很简单的,我主要是用来控制游戏中背景音乐和音效,Master主音量,Music和Effect对应的是背景音乐和音效,正如Gourps看到的父子关系一样,主音量控制背景音乐和音效的,就像爸爸可以管儿子是不是和下图系统的音量合成器有点像
- 最后再附上对应的脚本看看
/**
*Copyright(C) 2019 by #PROJECTNAME#
*All rights reserved.
*FileName: #SCRIPTFULLNAME#
*Author: Nxf
*Version: #VERSION#
*UnityVersion:#UNITYVERSION#
*Date: #DATE#
*Description:
*History:
*/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
namespace BioVR.Service
{
public enum VolumType
{
MusicVolume,
EffectVolume,
MasterVolume,
}
public enum SoundType
{
Music,
Effect,
}
public interface ISoundService
{
//声音的开关
void ControlVolume(VolumType volumType, float _volume);
void PlaySound(AudioClip clip, bool loop);
void PlaySound(AudioSource source, AudioClip clip, bool loop, bool isEffectMixerGroup);
void PlaySoundLenth(AudioSource source, AudioClip clip, float time);
void Stop(AudioSource source);
void Stop();
}
public class SoundService : ISoundService
{
AudioSource _audioSourceSound;
AudioMixer _audioMixer
{
get
{
return SoundLoader.ExpmAudioMixer;
}
}
public SoundService()
{
}
public SoundService(AudioSource audioSourceSound, bool isEffectMixerGroup = true)
{
_audioSourceSound = audioSourceSound;
_audioSourceSound.outputAudioMixerGroup = _audioMixer.FindMatchingGroups("Master")[isEffectMixerGroup ? 2 : 1];
}
public void ControlVolume(VolumType volumType, float _volume)
{
_volume = Mathf.Clamp(_volume, -100.0f, 0.0f);
Debug.Log(_volume);
switch (volumType)
{
case VolumType.MusicVolume:
_audioMixer.SetFloat("MusicVolume", _volume);
break;
case VolumType.EffectVolume:
_audioMixer.SetFloat("EffectVolume", _volume);
break;
case VolumType.MasterVolume:
_audioMixer.SetFloat("MasterVolume", _volume);
break;
default:
break;
}
}
/// <summary>
/// 默认声音播放器
/// </summary>
/// <param name="clip"></param>
public void PlaySound(AudioClip clip, bool loop = false)
{
if (_audioSourceSound == null)
{
throw new NotImplementedException("没有找到AudioSource");
}
_audioSourceSound.clip = clip;
_audioSourceSound.loop = loop;
_audioSourceSound.Play();
}
/// <summary>
/// 指定声音播放器
/// </summary>
/// <param name="source"></param>
/// <param name="clip"></param>
public void PlaySound(AudioSource source, AudioClip clip, bool loop = false, bool isEffectMixerGroup = true)
{
if (source == null)
{
throw new NotImplementedException("没有找到AudioSource");
}
source.outputAudioMixerGroup = _audioMixer.FindMatchingGroups("Master")[isEffectMixerGroup ? 1 : 0];
source.clip = clip;
source.loop = loop;
source.Play();
}
/// <summary>
/// 停止播放
/// </summary>
/// <param name="source"></param>
public void Stop(AudioSource source)
{
if (source == null)
{
throw new NotImplementedException("没有找到AudioSource");
}
source.playOnAwake = false;
source.loop = false;
source.Stop();
}
public void Stop()
{
if (_audioSourceSound == null)
{
throw new NotImplementedException("没有找到AudioSource");
}
_audioSourceSound.playOnAwake = false;
_audioSourceSound.loop = false;
_audioSourceSound.Stop();
}
public void PlaySoundLenth(AudioSource source, AudioClip clip, float time)
{
if (source == null)
{
throw new NotImplementedException("没有找到AudioSource");
}
source.PlayOneShot(clip);
}
}
public class SoundLoader
{
public static AudioClip SoundPointClick
{
get
{
return LoadClip(SoundType.Effect, "/Common/PointClick");
}
}
public static AudioClip SoundBgMusic
{
get
{
return LoadClip(SoundType.Music, "/Common/SoundBgMusic");
}
}
public static AudioMixer ExpmAudioMixer
{
get
{
return LoadAudioMixer("Sounds/AudioMixer/ExpmAudioMixer");
}
}
private static AudioClip LoadClip(SoundType soundType, string pathName)
{
AudioClip aduioClip = Resources.Load<AudioClip>("Sounds/" + soundType.ToString() + pathName);
if (aduioClip == null)
{
throw new NotImplementedException(pathName + "声音片段未找到,请确定路径");
}
return aduioClip;
}
private static AudioMixer LoadAudioMixer(string pathName)
{
AudioMixer audioMixer = Resources.Load<AudioMixer>(pathName);
if (audioMixer == null)
{
throw new NotImplementedException("声音混合器未找到,请确定路径");
}
return audioMixer;
}
}
public class GameService
{
public GameService(string dad)
{
}
public GameService()
{
}
public void Open()
{
Debug.Log("dsadad");
}
}
}