1.首先他是個(gè)什么東西
-
他的創(chuàng)建方式Project->右擊創(chuàng)建->AudioMixer組件凉泄,這個(gè)東西只能和AudioSource結(jié)合使用秋泄,大概就是AudioSource通過(guò)混音器輸出聲音册着,可以對(duì)聲音進(jìn)行更細(xì)膩的控制斟冕。雙擊打開大概就如下圖所示荧琼,標(biāo)紅的地方關(guān)鍵點(diǎn)
QQ截圖20190505140729.png -
那下圖是我做好的一個(gè)混合器丽惶,亂七八糟的箭頭是啥意思那炫七,
QQ截圖20190505142029.png
- Gourps-這個(gè)就是相當(dāng)于幾個(gè)音響,音樂(lè)(AudioSource【OutPut】)從那個(gè)音響輸出蚊夫,對(duì)應(yīng)關(guān)系自己看一下就可以
- Add-同的音響的音效中間這個(gè)面板的塊上有個(gè)Add按鈕可以添加不同音效诉字,讓聲音聽起來(lái)酷,這里不是專業(yè)的只是試了一下。
- Exposed Paramters-這個(gè)東西就是一個(gè)接口相當(dāng)于音響的控制按鈕壤圃,通過(guò)腳本去控制這個(gè)值陵霉,比如上面的三個(gè)Volume就是對(duì)應(yīng)三個(gè)音響的音量。在那幾個(gè)圈圈上右擊選擇帶有exposed的可以在Exposed Paramters看到了
- 其實(shí)還是很簡(jiǎn)單的伍绳,我主要是用來(lái)控制游戲中背景音樂(lè)和音效踊挠,Master主音量,Music和Effect對(duì)應(yīng)的是背景音樂(lè)和音效冲杀,正如Gourps看到的父子關(guān)系一樣效床,主音量控制背景音樂(lè)和音效的,就像爸爸可以管兒子是不是和下圖系統(tǒng)的音量合成器有點(diǎn)像
QQ截圖20190505143907.png
QQ截圖20190505144334.png
- 最后再附上對(duì)應(yīng)的腳本看看
/**
*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
{
//聲音的開關(guān)
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>
/// 默認(rèn)聲音播放器
/// </summary>
/// <param name="clip"></param>
public void PlaySound(AudioClip clip, bool loop = false)
{
if (_audioSourceSound == null)
{
throw new NotImplementedException("沒(méi)有找到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("沒(méi)有找到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("沒(méi)有找到AudioSource");
}
source.playOnAwake = false;
source.loop = false;
source.Stop();
}
public void Stop()
{
if (_audioSourceSound == null)
{
throw new NotImplementedException("沒(méi)有找到AudioSource");
}
_audioSourceSound.playOnAwake = false;
_audioSourceSound.loop = false;
_audioSourceSound.Stop();
}
public void PlaySoundLenth(AudioSource source, AudioClip clip, float time)
{
if (source == null)
{
throw new NotImplementedException("沒(méi)有找到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 + "聲音片段未找到权谁,請(qǐng)確定路徑");
}
return aduioClip;
}
private static AudioMixer LoadAudioMixer(string pathName)
{
AudioMixer audioMixer = Resources.Load<AudioMixer>(pathName);
if (audioMixer == null)
{
throw new NotImplementedException("聲音混合器未找到剩檀,請(qǐng)確定路徑");
}
return audioMixer;
}
}
public class GameService
{
public GameService(string dad)
{
}
public GameService()
{
}
public void Open()
{
Debug.Log("dsadad");
}
}
}