-
Welcome to part 2 of
-
upgrading the Nightmares survival shooter project
-
from Unity 4 to Unity 5.
-
If you missed part 1 please check that out
-
before continuing with this video.
-
In this version of the Nightmares project
-
we have a pause menu that's been
-
created with the new UI system.
-
The pause menu is called up when you press
-
escape using a script called
-
Pause Manager, which is attached to menu canvas.
-
We'll look at this script in more detail
-
later in the video.
-
For more information about creating
-
this type of UI see the information linked below.
-
Before moving on to scripting we want
-
to do one more thing in our mixer.
-
We want to expose the volume
-
of the music group and the sound effects group
-
to script control so that we can set them directly
-
without using the snapshot system.
-
We'll click on the music group,
-
right click on the volume parameter
-
and choose Expose To Script.
-
We're also going to do this for the sound effects group.
-
Now in our exposed parameters list
-
we're going to name these two parameters.
-
We're going to call our first exposed parameter
-
musicVol
-
and our second
-
sfxVol.
-
To rename the parameters double click
-
on the parameter names.
-
Now that we've got the audio setup
-
in our project we're going to move on
-
to adding some scripting control.
-
Let's start by editing the pause manager script.
-
We're going to go to the menu canvas game object
-
and then double click on the pause manager script
-
to open it in mono develop.
-
Let's just quickly review what we have here.
-
We've added the name space declaration
-
using UnityEngine.UI
-
because we're using some of the functions
-
from the UI system including the canvas
-
variable type.
-
We're also checking if we're currently
-
running in the Unity editor
-
and if so we're adding the name space declaration
-
using Unity editor.
-
We've declared a variable of the canvas type
-
called Canvas, and this is the UI canvas.
-
In the start function we're getting a reference
-
to that using getComponent.
-
In our update function we're checking
-
to see if the escape key has been pressed.
-
If escape has been pressed
-
we'll flip the canvas from enabled to disabled
-
or visa versa.
-
We'll also call the pause function.
-
In the pause function we're setting
-
Time.TimeScale
-
We're doing this by checking if it's currently
-
set to 0, set it to 1
-
and otherwise set it to 0.
-
By setting Time.TimeScale to 0
-
we can freeze the motion of all of
-
the objects in our game.
-
We also have a public function called Quit
-
in which we're checking if we're using
-
the Unity editor we're going to do
-
EditorApplication.isPlaying=false
-
meaning it's going to stop playing the scene.
-
Or else we're going to call
-
Application.Quit
-
meaning if this was the build of our application
-
it would actually quit out to the desktop.
-
What we're going to do is we're going to
-
add some stuff to this script
-
to allow it to control
-
our audio mixer.
-
The first thing that we're going to add is another
-
name space declaration
-
using UnityEngine.Audio.
-
This is going to allow us to access
-
members of UnityEngine.Audio including the
-
audio mixer and the audio mixer snapshot classes.
-
We're going to add 2 public variables
-
of the audio mixer snapshot type.
-
We're going to call these Paused
-
and Unpaused.
-
These are going to correspond to the snapshots
-
that we created in the first video.
-
We're going to add a new public function under Paused.
-
We're going to call this function Lowpass.
-
In low pass we're going to check if
-
Time.TimeScale is currently equal to 0.
-
If Time.TimeScale is equal to 0
-
we're going to call the TransitionTo function
-
from the Paused snapshot.
-
We're going to parse in a floating point parameter
-
time to reach,
-
in this case 0.01 seconds.
-
If Time.TimeScale does not equal 0
-
we're going to call TransitionTo from
-
the Unpaused snapshot.
-
With our low pass function declared
-
we're now going to call it from inside
-
the pause function.
-
Let's save our script and return to Unity.
-
In the editor we'll see that our
-
pause manager script now has
-
2 new variables, Paused and Unpaused.
-
We're going to set those to our audio mixer
-
snapshots we created earlier using the asset picker.
-
We'll select the Paused snapshot
-
and the Unpaused snapshot.
-
Let's play our scene and give it a try.
-
Press escape and see that the paused snapshot
-
has been recalled, and then when we click resume
-
we can see that Unpaused has been recalled,
-
and so we can hear that it's working.
-
In the Paused menu
-
we have volume controls for the music
-
and for the sound effects.
-
We're going to use those to control our audio mixer
-
using scripting. Let's start by creating a new C# script
-
which we'll call Mix Levels.
-
We'll double click it to open it in mono develop.
-
Again the first thing we're going to start by doing
-
is adding the name space declaration
-
using UnityEngine.Audio.
-
Next we're going to add a public variable
-
of the audio mixer type called Master Mixer.
-
Because we won't need either the update
-
or the start functions we're going to delete those.
-
We're going to declare a public function called
-
SetSfxLvl.
-
We're going to give SetSfxLvl a
-
parameter of the type float
-
called sfxLvl.
-
We're going to use the sfxLvl
-
parameter to parse a floating point value
-
to master mixer and use
-
that to change the volume
-
of our sound effects level in our audio mixer.
-
We're going to do this using the SetFloat function.
-
SetFloat takes two parameters.
-
A string, which is the name
-
of the exposed parameter that we want to set.
-
And a floating point parameter to set the value.
-
We're going to parse in the string sfxVol
-
which is the name of the parameter that we
-
exposed earlier in the inspector.
-
We're also going to declare a second public function
-
called SetMusicLvl.
-
SetMusicLvl is also going to take
-
a floating point parameter, in this case called
-
musicLvl.
-
In SetMusicLvl we're also going to use SetFloat
-
to set the value of our exposed parameter musicVol.
-
Let's save our script and return to the editor.
-
In the editor let's add our
-
Mix Level script to
-
our menu canvas game object.
-
We're also going to select
-
our audio mixer master mixer
-
as the master mixer variable.
-
Next, in the paused panel
-
we're going to choose our effects
-
slider game object.
-
In the slider script you'll see that
-
previously onValueChanged was
-
changing the value of the gun barrel end
-
and the player audio sources directly.
-
Now we can reconnect those to our audio mixer.
-
First we're going to drag in our menu canvas.
-
We're going to choose from our list of functions
-
found on this game object
-
our mix level script
-
and we're going to choose the SetSfxLvl function
-
from that script.
-
We can remove the connection to the player audio
-
source by selecting it and hitting -.
-
We're going to do the same thing for our music slider.
-
Drag in our menu canvas game object.
-
Choose from our list of functions, in this case
-
mix levels, set music level.
-
Because we determined that -10
-
decibels was probably the loudest
-
we wanted our music to be in the mixer
-
we're going to set that in the max value in the slider.
-
We're also going to set the minimum value
-
to -80, which will be silent.
-
In the effects slider we're going to do the same thing
-
except it's going to be between -80
-
and 0.
-
With that setup let's play our scene
-
and give it a try.
-
We'll hit escape,
-
adjust the music volume,.
-
Let's pull down the sound effects volume too.
-
So there we have it.
-
For more information about audio mixers,
-
snapshots, exposed parameters and effects
-
see the information linked below.