In Unity it's possible to directly control
parameters of an audio mixer
like levels and effects settings
via script at runtime.
To set a single mixer parameter via script
we'll use the SetFloat function.
SetFloat has two parameters,
a string for the name of the exposed
parameter we're going to be setting
and a float for the value to set it to.
SetFloat is called from an audio mixer.
In order for a parameter to be controlled
using SetFloat it has to be exposed to script control.
First make sure that the parameter is visible in the inspector.
Then in the inspector right click on the parameter name.
Here we'll expose volume for the master group of master mixer.
Right click and choose Expose To Script.
When this is done you'll see a little arrow appear beside the name.
You'll also notice that it's been added
to our list of exposed parameters
for the master mixer audio mixer asset.
If we click on the list of exposed parameters
we'll see two parameters that have already been exposed,
Music Vol and SFX Vol which correspond to the volumes of
the music group and the sound effects group
of master mixer.
And we have a third called My Exposed Param.
My exposed param is our new parameter
and we can see that it's the master volume.
If we double click on it we can set it's name.
The name that we choose is the
string that we'll parse to SetFloat
when setting the value via script.
Here's one example of how we can use SetFloat,
in this case to control the music volume
and sound effects volume balance in our game.
Here we have the Nightmares project which is available
as a free download from the assets store.
And we've added a simple pause menu with some
audio options to it. Let's check it out.
When we hit escape our pause menu comes up
and here we can adjust the music volume,
and the sound effects volume.
Notice how the new values are reflected in the mixer.
Now that we've seen how to expose parameters to script control
let's look at setting their values via script.
On our game object audio mixer control
we have a script called Mix Levels.
In our mix levels script we have
the namespace declaration using
UnityEngine.Audio
And what this allows us to do is to access
classes like audio mixer, which are members
of UnityEngine.Audio.
We also have a public variable
of the audio mixer type called masterMixer.
We have two public functions, SetSfxLvl,
which takes a parameter of the type float called SfxLvl.
And then in that function we're going to called the SetFloat function
from master mixer and we're going to parse in our string
SfxVol saying that's the parameter we want to address
and we're going to parse in our floating
point value SfxLvl, which is what the value is going to be set to.
In our public function SetMusicLvl
we're going to do the same thing, we're going to parse in
our float musicLvl and we're going to use that to call
SetFloat for masterMixer and parse in the string
musicVol saying we want to address the musicVol exposed parameter
and set the value
using our float music Lvl.
In the Unity editor we've dragged
our master mixer audio mixer
to our master mixer variable slot.
In order to set the floating point values in mix levels
we're using the UI system.
Here in our menu canvas we've got two sliders,
here's our effects slider.
In the slider script of our effects slider
we're choosing our audio mixer control
from our list of objects in the scene
with the audio mixer control game object selected
we're going to address our mix level script
and set SFX level within that
to parse the value of the slider to SetFloat.
For our music slider we've gone through the same
process but in this case setting it to
the float of SetMusicLvl.
When working with exposed parameters it's important to note
that exposing a parameter and setting it's value
using SetFloat removes it from control
of the audio mixer snapshot system.
The audio mixer snapshot system allows us to recall the
entire state of the mixer by toggling between
one snapshot and another.
It's possible to return a parameter to snapshot control
using the ClearFloat function.
ClearFloat is called from an audio mixer
and takes a parameter of the type string,
which specifies the parameter that we want to release
from snapshot control on that mixer.
For more information about snapshots and
controlling them via script
please see the information linked below.
Subtitles by the Amara.org community