Fungus Game: Sound & Music Setup

Fungus is a great tool, so great that its users sometimes even forget they are building on a complete game engine Unity. There are many unknown features of Unity that makes me sad sometimes. One such feature is Audio Mixer, Unity’s solution for controlling audio and applying cool effects. So in this post I will briefly review how audio works on Unity, what Fungus does to play these sounds and how you can connect Fungus to Unity Audio Mixer.

3D vs 2D Sound

First of all let me remind you that Unity was a 3d engine first. It is still in its website name. As such Unity has a 3D audio system where sounds’ volume and direction are managed based on their distance to the player. If you are using Fungus for a 2D game this may not interest you much as by default all Fungus sounds are 2d. However you can still get creative with a 3D sound setup even in a 2D game. Rustling chain sounds playing at the back of players head (assuming he is using a surround system or headset) moving closer slowly and than Baaam!!! An ghost appears on the screen.

AudioListenerOnCamera3D sound is all about two components AudioListener and AudioSource. Unity adds AudioListener by default on Main Camera when you create a new scene so you may never recognize it is there. This represent where player is on the 3D coordinate system. There can only be one active AudioListener on the scene at the same time and all other 3D sounds are calculated based on their distance to this component.

AudioSource is where sound originates. For instance you can put an AudioSource on the feet of an enemy so sound will come from the feet when you play walking sound . I don’t think I need to explain it much, anyone who played a 3D game would be familiar with the concept. The important point is that each AudioSource has its own configuration. You can adjust their volume, you can set them to be 2d or 3d, you can give them doppler effect so if they are moving fast you will hear their sound stretched and squished like a passing car.

Where are Fungus Audio Sources?

There are many places Fungus create sounds. Two of them are PlaySound and PlayMusic commands. These commands will create an game object called MusicController with an AudioSource when you first call them. You don’t have direct access to this object before running the game. However you can use commands like SetVolume and SetPitch to modify its two values. Music controller in this sense do not offer much for customization.

SayDialogAudioSourceAnother AudioSource can be located in SayDialog prefab. This one is used for character text typing sounds, button clicks etc. Here you can access SayDialog before running the game and modify volume of text typing separately from sound effects and music.

The most customizable audio command available out of box is ControlAudio command. Because you can simply put an AudioSource on your scene configure it in whatever means you want and then play it from Flowchart.

Enter the Mixer

AudioMixer

With audio mixer you can create complex audio setups with layers and effects. Here is a brief summary to get you acquainted. Mixers contain one or many Groups. Each group has its own volume slider with values between -80db to 20db. These values are actually gain values, with 0 being no gain (play sound as is) +20db is 20 decibel boost (play sound in an higher volume then it is recorded). You can connect mixers by sending output sound on one mixer to a group under another mixer. This way you can create a sound hierarchy where you can start an sound effect that is first adjusted by its own sound group then adjusted by parent mixer.

In above screenshot you can see a mixer setup where a Main Mixer controls 3 sub mixers’ volume levels. This setup lets you adjust volume of every sound played under these sub mixers via single slider(or in case of changing it via script one parameter). You can put these controls on an option menu so player can adjust sounds as he pleases.

So how do you use these mixers. Previously mentioned AudioSources all have an output parameter. By default this parameter is empty, so no mixer controls that sound source. However you can choose a group for that parameter and voila! Your sound is now directed to the mixer before outputting its glory. For instance you can connect SayDialog writing sound to a group in effects mixer. When Fungus starts playing writing sounds those sounds first go to the mixer. If player have put the effects volume down, writing sounds will also play on lower volume.

There are lots of other things you can do with mixers. Unity has plethora of sound effects ranging from chorus to distortion. A professional sound designer would know the best. I am not so I won’t attempt to explain these effects. Just know it is there and think of some interesting ways to use them. For instance one of the audio effects I use in my games is the low pass filter. It creates an effect similar to hearing music from outside of the club. I activate the effect when player pauses the game so music keeps playing on the background a bit muffled.

How to Use Audio Mixers With Fungus

ExposingAudioParameterUnity’s audio mixers are like black boxes. You do not have access to parameters by default. But you can expose mixer parameters for to manuplate them via script. Simply select an mixer group and on the inspector right click on the parameter you want to expose. Give that parameter a name which you will reference later on Fungus command. Here I have exposed volume parameter and gave it “MusicMasterVolume” name.

After that you can use audioMixer.SetFloat("MusicMasterVolume", volume);to adjust this parameter. But of course that will require you to write a bit of code yourself. Why not make a reusable command for that?! So I did write a command to set an audio mixer parameter. I won’t go into detail on writing a custom command here. If you are interested, I did write on that topic in a previous post.

// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
using UnityEngine;
using UnityEngine.Audio;
namespace Fungus
{
/// <summary>
/// Sets parameter value for target AudioMixer.
/// </summary>
[CommandInfo("Audio",
"Set Mixer Parameter",
"Sets parameter value for target AudioMixer.")]
[AddComponentMenu("")]
public class SetMixerParameter : Command
{
[Tooltip("Target AudioMixer")]
[SerializeField]
protected AudioMixer mixer = null;
[Tooltip("Name of exposed parameter")]
[SerializeField]
protected string parameterName = "";
[Tooltip("New float value of the parameter")]
[SerializeField] protected float parameterValue = 0f;
public override void OnEnter()
{
mixer.SetFloat(parameterName, parameterValue);
Continue();
}
public override string GetSummary()
{
if (mixer == null)
{
return "No target mixer selected";
}
return "Set " + parameterValue + " of " + mixer.name + " to " + parameterValue;
}
public override Color GetButtonColor()
{
return new Color32(242, 209, 176, 255);
}
}
}
view raw SetMixerParameter.cs hosted with ❤ by GitHub

SetAudioMixerParameterWith above code you can adjust an exposed parameter directly from the flowchart. Here in the screenshot I am reducing the music volume by 20 decibels.

Snapshots for Quick Transitions

TransitionToSnapshotAnother cool feature of AudioMixers are snapshots. With snapshots you can preconfigure settings which you can switch between via simple command call. When you create a mixer it will come with a default snapshot named “Snapshot”. You can edit this snapshot and create new snapshots as you wish. For instance I have created a MuteAll snapshot where volumes of all mixer groups are set to -80db. I have also written a command for this.

// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
using UnityEngine;
using UnityEngine.Audio;
namespace Fungus
{
/// <summary>
/// Transitions mixer values to selected snapshot.
/// </summary>
[CommandInfo("Audio",
"Transition to Mixer Snapshot",
"Transition mixer values to selected snapshot.")]
[AddComponentMenu("")]
public class TransitionToMixerSnapshot : Command
{
[Tooltip("Target AudioMixer")]
[SerializeField]
protected AudioMixer mixer = null;
[Tooltip("Name of snapshot")]
[SerializeField]
protected string snapshotName = "";
[Range(0,60)]
[Tooltip("Transition time")]
[SerializeField] protected float transitionTime = 1f;
public override void OnEnter()
{
AudioMixerSnapshot snapshot = mixer.FindSnapshot(snapshotName);
if (snapshot == null)
{
Continue();
return;
}
snapshot.TransitionTo(transitionTime);
}
public override string GetSummary()
{
if (mixer == null)
{
return "No target mixer selected";
}
return "Transition to " + snapshotName + " of " + mixer.name + " in " + transitionTime + "seconds";
}
public override Color GetButtonColor()
{
return new Color32(242, 209, 176, 255);
}
}
}

And there you go. A brief introduction to Audio in Unity and how you can use it. You can copy the code and use it as is without modifying anything.

 



About the author

Sercan Altun: Hello there fellow human, I am Sercan Altun: video game developer, coding aficionado & part-time weirdo. Check out my blog on game development and stuff.

B801 300D 93CC 11A9 3332 4270 A5FB C300 1020 24E7