< Return to Video

AudioMixer Exposed Parameters - Unity Official Tutorials

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

more » « less
Duration:
05:30

English subtitles

Revisions