In Unity it's possible to save and recall
the state of an audio mixer asset
using snapshots.
In this audio mixer master mixer
we have two snapshots created.
In this case Unpaused and Paused.
In Unpaused we've set our levels
and we've also set the cutoff frequency
of a low pass filter,
in this case to 22000hz.
When we click on the Paused snapshot
we'll see that a new cutoff frequency
has been recalled, 365hz.
In this case it's attenuating the high
frequencies of our music track,
which is running through the music group of master mixer.
When the Escape key is pressed
the music track will be low pass filtered.
Let's check that out.
When we press escape our music track
is low pass filtered and when we press again.
To create a new snapshot we can click
the + button in the snapshots area.
Now we have a copy of Unpaused
and we can set our values, and now
we can recall those values in the editor
by clicking on the snapshot names.
At runtime we can recall snapshot values
using the TransitionTo function.
TransitionTo takes a float in seconds
and allows us to interpolate
between one snapshot and another.
TransitionTo is called
from the audio mixer snapshot
that we're transitioning to.
In this project we've got a game object
called Menu Canvas.
Menu Canvas has a script
called Pause Manager.
Let's take a look at Pause Manager in mono develop.
In Pause Manager we've added the
namespace declarations using UnityEngine.UI
and using UnityEngine.Audio.
We are also checking to see if
we're currently using the Unity editor
and if so we'll include the
Unity editor namespace.
Inside our class we're declared two public
variables of the audio mixer snapshot type
called Paused and Unpaused.
We've also declared a variable of the type canvas
called Canvas.
In our start function we're getting a reference
to our canvas component using GetComponent.
In update we're checking to see if the escape
key has been pressed.
If it has we'll flick the enabled
state of canvas
to Enabled if it's disabled
or Disabled if it's enabled.
We'll also call the Pause function.
In Pause we're going to check
to see if time.timeScale
is equal to 0.
And if it's not we're going to set it to 0.
If it is we'll set it to 1.
Next we're going to call the low pass function.
The low pass function also checks to see if
time.timeScale is equal to 0
and if it is it will call the TransitionTo function
from the Paused snapshot.
We're going to parse in the floating point parameter
time to reach, which in this case
is 0.01 seconds.
If time.timeScale is not equal to 0
we're going to call TransitionTo
from the Unpaused snapshot,
also parsing in that same
0.01 second parameter
for the time to make the transition
We have a public function called Quit which is going to check
if we're using the Unity editor
and if we are it's going to set
EditorApplication.isPlaying to false,
meaning it's going to stop playing our scene.
If we're not using the editor, meaning this is a
build of our game we're going to call
application.Quit, meaning we're going to
quit the application out to the desktop.
In the Unity editor we've assigned the Paused and
the Unpaused snapshots
to the audio mixer snapshots that we created
in master mixer here.
If we want to change these we can do so
using the asset picker.
When transitioning between snapshots
the default interpolation curve is linear.
We can change that by selecting a parameter,
right clicking on it and choosing one of these
override transition types.
The smooth step snapshot transition type
will give us an S shaped transition curve.
Squared will give us a parabolic curve.
Square root will give us a square root curve.
Brick wall start will immediately transition
to the stored value being transitioned
to at the beginning of the transition.
Brick wall end will wait until the
transition time has elapsed and then
make a hard transition to the stored value.
In addition to using TransitionTo
it's also possible to transition
to an interpolated blend
of multiple snapshots using the transition
to snapshots function.
Transition to snapshots function takes 3 parameters.
The first, an array of audio mixer snapshots
allows us to choose which snapshots
we want to create a blend between.
Second parameter is an array of floats.
It allows us to specify the weighting
of each element in the resulting blend.
The third parameter is a float
in seconds which allows us to specify
the time to reach the new desired blend.
Transition to snapshots is called
from the audio mixer that contains the snapshot
that we're transitioning between.
What we've got here is we've setup 3 cube triggers.
When our player collides with each one of these
we will transition to a blend
of 2 snapshots.
Each of the snapshots has been setup
in our sound effects sub mixer.
The sound effects sub mixer is running in to the sound effects
group of our master mixer.
I've turned down the music for this example.
In the sound effects mixer, on the gun shots group
we've added a send effect.
This is under Add Send
and what this will do is send a copy of the
signal from gun shots to the receive effect
that we've added to reverb return.
The receive effect will pass it's signal to our
SFX reverb effect which will cause our gunshots
to sound like they're happening in a reverberant space.
We've setup two snapshots.
One with no reverb in which the send level
for our gun shots send effect
is turned all the way down.
The next is heavy reverb
where it's turned all the way up,
and we can just listen to how those sound.
Edit In Play Mode.
Click, we can hear no reverb.
Select heavy reverb.
I've intentionally put a very strong unrealistic
reverb on there so that we can hear the effect clearly.
Now we've created these three
transparent cubes called reverb trigger 1, 2 and 3.
Each of them has a copy of the script reverb
trigger on it.
Reverb trigger is a simple script
which contains a public variable for our
reverb control script, and a public
integer called trigger number.
When the player collides with the trigger
that this is attached to it's going to call
the blend snapshot function of
reverb control, which takes an integer trigger number.
In Unity we've assigned the reverb
control variable by dragging and dropping
our audio mixer control object,
which has that script attached to it.
We've also assigned unique trigger numbers
to each of these trigger objects.
On our audio mixer control game object we have
our reverb control script.
In our reverb control script we have
the namespace declaration using UnityEngine.Audio.
This allows us to access members
of UnityEngine.Audio like the audio mixer,
and audio mixer snapshot classes.
We've declared a public variable of the audio mixer type
called Mixer and also an array
of audio mixer snapshots
called Snapshots.
We've also declared a public array of floats
called Weights and we're going to use these
to specify the weightings of each of the snapshots
as we blend between our different snapshot states.
We have a public function called BlendSnapshot,
which takes a parameter of the type int called TriggerNr.
This is the int that we're getting from our
reverb trigger script when the player collides with the collier.
We have a switch statement here which takes
trigger number and chooses which of the
colliders we've collided with,
case 1, case 2 and case 3.
In each of these cases we're going to set
the weights of the weight array to correspond
to the percentage of which
snapshot we want to contribute to the blend.
In case 1 which will be selected if they player triggers
the right-most red trigger collider.
We'll set the weights of our weights array
to 1.0 and 0.
What this will mean is that our resulting snapshot
blend which will be created
when we call mixer.transitionToSnapshot
is a blend which is 100% of the snapshot
at 0 in the snapshots array
and 0% of the snapshot at 1 in the snapshots array.
In case 3 we're doing the opposite thing, in this
case we're going to have 0% of our no reverb
snapshot and 100% of our heavy reverb snapshot.
But case 2 is where things
get interesting and where transition to snapshots
becomes really useful.
Here we're creating a third reverb state
by transitioning to this blend
of 25% of our no reverb snapshot
and 75% of our heavy reverb snapshot.
So we've created a third reverb state
by transitioning to a blend of our
two existing snapshots.
In Unity we've assigned the variables
of our reverb control script
by dragging in our sound effects mixer asset
to our mixer variable slot.
We've also assigned the snapshots
by clicking and selecting them from the list.
Here we have our no reverb snapshot,
and here we have our heavy reverbs snapshot.
We've also set the size of our weights
array to 2 so that it will
match up to the size of
our snapshots array and so that these
lists of elements can line up.
We haven't initialised the values here because
those are going to be set by the script at run time.
As we give this a try pay attention to the reverb
return level on our gun shots track
and watch how it changes as our
character moves through each of the different
triggers, let's give it a try.
So what we've done here is we've used
our two snapshots no reverb and heavy reverb
to create this third blended snapshot
represented by the purple collider.
And, so we've chosen to show reverb here
but this technique is applicable to all
sorts of audio states and really any type of
mixer state that you can save
in a snapshot can be blended
and recalled using this same technique.
Subtitles by the Amara.org community