How to create your first crossfading Parameter Command
This continues from where we left off with How to create your first Playlist. Make sure you've done those steps first.
Real-Time Parameter Commands are used when you have complex audio behaviour that needs to be driven by in-game changes or built-in Parameters such as distance from the Listener.
Note: Real-Time Parameter Commands feature is only available in Master Audio 2024+.
-
Locate and select the Master Audio game object in the Scene. Go down to the Real-Time Parameter Commands section. Expand it if it isn't already by clicking the arrow.
-
For the simplest example, we will use the built-in Parameter "[Seconds Since Command Invoked]", so we can skip creating a Parameter in the top section "Parameter Management". Go ahead and collapse that section by clicking the arrow.
-
Next, let's create a new Parameter Command. Click the "Add Parameter Command" button, then select from the Parameter dropdown "[Seconds Since Command Invoked]". More fields will appear underneath.
-
We want the crossfade to involve 2 diferent sounds, so let's add 2 elements. Click the "Add Element" button twice.
-
We will play 2 different Variations of the Sound Group you created in the Quick Start How to create your first Sound Effect with variations, and crossfade between them. So, go ahead and select the Sound Group "Scream" in the Sound Group dropdown in both Elements.
-
To select a specific Variation for each Element, you will need to first check the "Advanced Controls" checkbox. Then select "Play Specific" in the Variation Mode dropdown. Then Enter the Variation's name in the Variation Name field. Do this for both Elements, using a different Variation for each.
-
Next, we will set the volume curve for each Variation, so that it crossfades between the two. First, we'll need to adjust the display range of our Volume curve window. Up at the top of your Command, there are fields for Curve X Min/Max. Change the range to 0-15 by setting Curve X Max to 15.
-
While we're up here, let's give the Parameter Command a proper name instead of "NewParameterCommand1". Click the gear icon next to the name, change the yellow background text to "Scream crossfade", and click the disc icon to save changes. Yes, you can rename "YourElement1" and the other Elements the same way we just did the Command name.
-
Time to set the Volume Curve for Element 1. Click the black bar for Volume Curve to open the Animation Curve editor window.
-
You can edit the Animation Curve as normal by using presets, adding keys and other Unity features here. You can also click "Fade In/Out Fields" and use the numeric text boxes to set up the fade. Let's try that. Click Fade In/Out Fields. Set the fields as below, then click the "Set Curve to Fade In/Out Fields" button. You'll see a green thumbnail of the Volume Cuve above and can click it to blow it up for further editing.
-
Let's do the Volume Curve for Element 2, using the numeric textboxes again. We will set it to fade in during the fade out of Element 1, as follows, with a slower fade out at the end.
-
Go ahead and collapse your elements by clicking "Collapse All" since we're done editing them.
- Time to test out the crossfade Command! Press Play and notice that there's a button on the Parameter Command title line that you can click to Invoke the Command. It says "Invoke!". Go ahead and click it, and listen. After 7 seconds, the first Element's sound will fade out as the 2nd fades in!
-
However, the Command didn't end. You can also see in the Inspector at runtime the number of active instances, and it stayed at 1. It kept running after the crossfade was complete, and would need to be stopped someone. If we want it to end at a certain Parameter value, we can set that up. Press stop and exit Play mode.
-
Under the Parameter dropdown, there's a checkbox for "Stop Cmd Out Of Range". Check that box, and enter the valid range for the Command to be active. We'll go ahead and match the Curve X Min/Max. That means that if the selected Parameter is ever evaluated as less than 0 or greater than 15, the Command will stop.
-
Let's try that. Press Play, Invoke the Command, and make sure it stops after 15 seconds. You should see "Active: 0" after that amount of time. Congrats! You have created a working Parameter Command that executes and stops itself.