< Return to Video

Unity 5 Audio: Upgrading Unity 4 Project Audio Part 2: Snapshots and Exposed Parameters

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

more » « less
Duration:
09:46

English subtitles

Revisions