Exposed AudioMixer Parameters
Checked with version: 5
In Unity it's possible to control the parameters of an AudioMixer directly by exposing them to script control. Parameters that have been exposed to script control can be set using the SetFloat function.
Exposed AudioMixer Parameters
- 00:00 - 00:03
In Unity it's possible to directly control
- 00:03 - 00:06
parameters of an audio mixer
- 00:06 - 00:08
like levels and effects settings
- 00:08 - 00:11
via script at runtime.
- 00:11 - 00:15
To set a single mixer parameter via script
- 00:15 - 00:18
we'll use the SetFloat function.
- 00:18 - 00:20
SetFloat has two parameters,
- 00:20 - 00:23
a string for the name of the exposed
- 00:23 - 00:24
parameter we're going to be setting
- 00:24 - 00:28
and a float for the value to set it to.
- 00:28 - 00:32
SetFloat is called from an audio mixer.
- 00:32 - 00:34
In order for a parameter to be controlled
- 00:34 - 00:40
using SetFloat it has to be exposed to script control.
- 00:40 - 00:44
First make sure that the parameter is visible in the inspector.
- 00:45 - 00:50
Then in the inspector right click on the parameter name.
- 00:50 - 00:53
Here we'll expose volume for the master group of master mixer.
- 00:55 - 00:59
Right click and choose Expose To Script.
- 01:01 - 01:04
When this is done you'll see a little arrow appear beside the name.
- 01:04 - 01:08
You'll also notice that it's been added
- 01:08 - 01:10
to our list of exposed parameters
- 01:10 - 01:14
for the master mixer audio mixer asset.
- 01:14 - 01:17
If we click on the list of exposed parameters
- 01:17 - 01:20
we'll see two parameters that have already been exposed,
- 01:20 - 01:23
Music Vol and SFX Vol which correspond to the volumes of
- 01:23 - 01:25
the music group and the sound effects group
- 01:25 - 01:27
of master mixer.
- 01:27 - 01:30
And we have a third called My Exposed Param.
- 01:30 - 01:32
My exposed param is our new parameter
- 01:32 - 01:35
and we can see that it's the master volume.
- 01:36 - 01:39
If we double click on it we can set it's name.
- 01:39 - 01:41
The name that we choose is the
- 01:41 - 01:43
string that we'll parse to SetFloat
- 01:43 - 01:46
when setting the value via script.
- 01:46 - 01:49
Here's one example of how we can use SetFloat,
- 01:49 - 01:52
in this case to control the music volume
- 01:52 - 01:56
and sound effects volume balance in our game.
- 01:56 - 01:59
Here we have the Nightmares project which is available
- 01:59 - 02:01
as a free download from the assets store.
- 02:02 - 02:05
And we've added a simple pause menu with some
- 02:05 - 02:07
audio options to it. Let's check it out.
- 02:13 - 02:16
When we hit escape our pause menu comes up
- 02:16 - 02:18
and here we can adjust the music volume,
- 02:20 - 02:22
and the sound effects volume.
- 02:23 - 02:26
Notice how the new values are reflected in the mixer.
- 02:34 - 02:37
Now that we've seen how to expose parameters to script control
- 02:37 - 02:39
let's look at setting their values via script.
- 02:41 - 02:44
On our game object audio mixer control
- 02:44 - 02:47
we have a script called Mix Levels.
- 02:47 - 02:49
In our mix levels script we have
- 02:49 - 02:51
the namespace declaration using
- 02:51 - 02:53
- 02:53 - 02:55
And what this allows us to do is to access
- 02:55 - 02:58
classes like audio mixer, which are members
- 02:58 - 03:00
- 03:00 - 03:02
We also have a public variable
- 03:02 - 03:06
of the audio mixer type called masterMixer.
- 03:06 - 03:09
We have two public functions, SetSfxLvl,
- 03:09 - 03:13
which takes a parameter of the type float called SfxLvl.
- 03:14 - 03:18
And then in that function we're going to called the SetFloat function
- 03:18 - 03:21
from master mixer and we're going to parse in our string
- 03:21 - 03:25
SfxVol saying that's the parameter we want to address
- 03:25 - 03:27
and we're going to parse in our floating
- 03:27 - 03:31
point value SfxLvl, which is what the value is going to be set to.
- 03:31 - 03:33
In our public function SetMusicLvl
- 03:33 - 03:35
we're going to do the same thing, we're going to parse in
- 03:35 - 03:39
our float musicLvl and we're going to use that to call
- 03:39 - 03:42
SetFloat for masterMixer and parse in the string
- 03:42 - 03:45
musicVol saying we want to address the musicVol exposed parameter
- 03:45 - 03:47
and set the value
- 03:47 - 03:49
using our float music Lvl.
- 03:49 - 03:52
In the Unity editor we've dragged
- 03:52 - 03:55
our master mixer audio mixer
- 03:55 - 03:57
to our master mixer variable slot.
- 03:58 - 04:02
In order to set the floating point values in mix levels
- 04:02 - 04:04
we're using the UI system.
- 04:05 - 04:08
Here in our menu canvas we've got two sliders,
- 04:08 - 04:10
here's our effects slider.
- 04:10 - 04:13
In the slider script of our effects slider
- 04:13 - 04:17
we're choosing our audio mixer control
- 04:17 - 04:19
from our list of objects in the scene
- 04:19 - 04:22
with the audio mixer control game object selected
- 04:22 - 04:25
we're going to address our mix level script
- 04:25 - 04:28
and set SFX level within that
- 04:28 - 04:33
to parse the value of the slider to SetFloat.
- 04:33 - 04:35
For our music slider we've gone through the same
- 04:35 - 04:38
process but in this case setting it to
- 04:38 - 04:41
the float of SetMusicLvl.
- 04:41 - 04:45
When working with exposed parameters it's important to note
- 04:45 - 04:48
that exposing a parameter and setting it's value
- 04:48 - 04:51
using SetFloat removes it from control
- 04:51 - 04:54
of the audio mixer snapshot system.
- 04:55 - 04:58
The audio mixer snapshot system allows us to recall the
- 04:58 - 05:01
entire state of the mixer by toggling between
- 05:01 - 05:03
one snapshot and another.
- 05:03 - 05:07
It's possible to return a parameter to snapshot control
- 05:07 - 05:09
using the ClearFloat function.
- 05:09 - 05:13
ClearFloat is called from an audio mixer
- 05:13 - 05:16
and takes a parameter of the type string,
- 05:16 - 05:19
which specifies the parameter that we want to release
- 05:19 - 05:22
from snapshot control on that mixer.
- 05:22 - 05:24
For more information about snapshots and
- 05:24 - 05:26
controlling them via script
- 05:26 - 05:28
please see the information linked below.