Game Components

4. Game Components

4.1 Studio Listener

StudioListener.cs
alt text

The FMOD Studio Listener script must be attached to your camera for 3D positioned events to pan correctly.

FMOD Studio supports up to eight listeners. If you have game with multiple camera's such as a split-screen multiplayer, each listener will be assigned a Listener Number at runtime which represents it's index in the array of listeners (ie. from 0 to 7).

You can change the position used for attenuation by attaching an object to the Attenuation Object, this can be useful for third person/top down games.

If no listener exists at the start of a scene one will be created.

4.2 Studio Event Emitter

StudioEventEmitter.cs
Emitter

The FMOD Studio Unity Integration comes with a simple emitter component to play a FMOD Studio Event at a given world position. Each emitter creates an instance of the selected event, and manages that Event Instance over the course of its lifetime. All additional options in the emitter component only affect the Event Instance that the emitter is responsible for.

4.2.1 Creating New Emitters

If you drag an Event from the Event Browser onto the scene it will create a new Game Object with an Emitter component already attached.

4.2.2 Place Emitters on Existing Game Objects

Use the Add Component button and select FMOD Studio Event Emitter. You can add multiple Emitters to a single Game Object.

4.2.3 Select the Event

Enter the full path (starting with event:/ e.g. event:/Weapons/Full Auto Loop) into the text box. You can press the search icon next to the box to bring up an Event search window. Alternatively, you can drag an Event from the Event Browser onto the text box to set the Event.

The Open In Browser button will highlight the currently selected event in the Event Browser. In the case that an event is not currently selected, the Event Browser window will be opened instead.

4.2.4 Event Play Trigger and Event Stop Trigger

You can set up the Emitter to automatically start or stop the Event in response to Unity events.

See the Trigger Conditions for more.

4.2.5 Starting and Stopping from script

If you wish to start or stop the Emitter from code set the option to None. Then in a script attached to the same Game Object add the line

SendMessage("Play");
SendMessage("Stop");

or

GetComponent<FMODUnity.StudioEventEmitter>().Play();
GetComponent<FMODUnity.StudioEventEmitter>().Stop();

Using SendMessage will allow you to start multiple Emitters. GetComponent is more efficient if you store the StudioEventEmitter reference and re-use it multiple times.

4.2.6 One Shot Events

If the Event is a one-shot then each call to Play from script or in response to a Unity event will spawn a new instance. Stopping will only stop the most recent instance. Looping events will restart if Play is called a second time without stopping.

4.2.7 Overriding Attenuation

If you tick the box to allow overriding attenuation, then you will be able to enter new minimum and maximum falloff distances for the Emitter. You will also be able to change the distances using the sphere handles in the scene view.

Note that the values entered here will not affect a spatializer on the event that is overriding with its own values.

4.2.8 Parameters

You can customise each Emitter with different Parameter values to be set before the Event is started.
To control parameter values after the Event has started, use the Studio Parameter Trigger.

4.2.9 Advanced Options

Preload Sample Data

The audio sample data for this Event will be loaded into memory at level start and kept in memory for duration. This can reduce the latency in triggering the Event.

Allow Fadeout

The Allow Fadeout tickbox, under the Advanced Settings foldout, will let you decide if the Event will follow the AHDSR modulation when stopped or will cut off immediately.

Trigger Once

If selected then only the first attempt to Play the Event will succeed.

Non-Rigidbody Velocity

If enabled, the velocity of the GameObject the Emitter is attached to will be calculated by FMOD, allowing for the use of Doppler, and Speed (Relative) and Speed (Absolute) built-in parameters, without the need for a Rigidbody.

4.3 Studio Bank Loader

StudioBankLoader.cs
Bank Loader

Before you can play an FMOD Studio Event you must load the Bank that contains it. On the Settings screen there are three options for bank loading at initialization.

If you want more control over loading you can set the Bank Loading to None and use the Studio Bank Loader component.
Bank Loading Options

4.3.1 Creating a New Bank Loader

If you drag a Bank from the Event Browser onto the scene it will create a new Game Object with a loader component already attached.

4.3.2 Place a Loader on Existing Game Objects

Use the Add Component button and select FMOD Studio Bank Loader.

4.3.3 Controlling Loading

By default when a Bank is loaded only the Event Metadata is loaded. Audio sample data is left unloaded until the Event is played. You can change the behaviour by ticking the Preload Sample Data. This will consume more memory but will reduce latency when first playing an Event.

4.3.4 Selecting Banks

Press the Add Bank button to add another slot for specifying a bank.

You can enter the name of Bank into the text box, or press the Search button to bring up a Bank Search Window, or drag a Bank from the Event Browser onto the text box.

4.3.5 Load and Unload Events

You can set up the Bank Loader to automatically load or unload the Banks in response to Unity events.

See the Trigger Conditions for more.

4.3.6 Loading and Unloading From Script

If you wish to start loading from code set the Load option to None. Then in a script attached to the same Game Object add the line

SendMessage("Load");

or

GetComponent<FMODUnity.StudioBankLoader>().Load();

For unloading the Unload option to None and then use the script

SendMessage("Unload");

or

GetComponent<FMODUnity.StudioBankLoader>().Unload();

SendMessage is simpler if you have multiple Bank Load components attached to the one Game Object.

4.4 Studio Parameter Trigger

StudioParameterTrigger.cs
alt text

The FMOD Studio Unity Integration comes with a component that will allow you to change Parameters on an Emitter in response to Unity events.

If you need a more control over parameter changes, such as updating them every frame or using calculated values, then you need to write script.

For example:

FMODUnity.StudioEventEmitter emitter;
void OnEnable()
{
    var target = GameObject.Find("NameOfEmitterObject");
    emitter = target.GetComponent<FMODUnity.StudioEventEmitter>();
}
void Update()
{
    float value = 1.0f; // calculate the value every frame
    emitter.SetParameter("ParameterName", value);
}

4.4.1 Place Parameter Trigger component on a Game Object

Use the Add Component button and select 'FMOD Studio > FMOD Studio Parameter Trigger'. You can add multiple Triggers to a single Game Object.

4.4.2 Select the Target Emitter

Press the search icon to show the object picker window. The object picker will show all game objects that contain an Emitter component. Alternatively you can drag a game object from the Hierarchy window onto the text box to set the Emitter.

4.4.3 Set the Parameter Values

Each Studio Event on the Emitter will be displayed. If you expand it there will be a list of Parameters on the Event. By default the Parameter will not be changed. Tick the check box if you want the Parameter to be modified, and set the desired value on the slider.

4.4.4 Triggering the Parameter Change

You can set up the component to automatically trigger the parameter change in response to Unity events.

See the Trigger Conditions for more.

4.4.5 Triggering Parameter Change from script

If you wish to trigger Parameter changes from code set the option to None. Then in a script attached to the same Game Object add the line:

GetComponent<FMODUnity.StudioParameterTrigger>().TriggerParameters();

4.5 Global Parameter Trigger

StudioGlobalParameterTrigger.cs
alt text

The Global Parameter Trigger works in much the same way the Studio Parameter Trigger does, but will only effect Global Parameters.

4.6 Trigger Conditions

Trigger condition

Object Start & Object Destroy

Object Start uses MonoBehaviour.Start, this is called on the frame when a script is enabled just before any of the Update methods are called the first time.
Object Destroy uses MonoBehaviour.OnDestroy, this occurs when a Scene or game ends.

Trigger Enter & Trigger Exit

Trigger Enter uses MonoBehaviour.OnTriggerEnter, if the Game Object has a collider attached (Box Collider, Sphere Collider) that is set to be a trigger, then you can trigger the event when another Object enters or leaves the collider.
Trigger Exit uses MonoBehaviour.OnTriggerExit, if you want only certain types of Objects to be able to trigger the event than you can select the Tag (e.g. Player).

Trigger Enter 2D & Trigger Exit 2D

Trigger Enter 2D uses MonoBehaviour.OnTriggerEnter2D.
Trigger Exit 2D uses MonoBehaviour.OnTriggerExit2D.

Collision Enter & Collision Exit

Collision Enter uses MonoBehaviour.OnCollisionEnter, if the Game Object has a collider attached then you can trigger the event change when another collider touches it.
Collision Exit uses MonoBehaviour.OnCollisionExit, if the Game Object has a collider attached then you can trigger the event change when another collider stops touching it.

Collision Enter 2D & Collision Exit 2D

Collision Enter 2D uses MonoBehaviour.OnCollisionEnter2D.
Collision Exit 2D uses MonoBehaviour.OnCollisionExit2D.

Object Enable & Object Disable

Object Enable uses MonoBehaviour.OnEnable, this function is called when the object becomes enabled and active.
Object Disable used MonoBehaviour.OnDisable, this function is called when the object becomes disabled.

Object Mouse Enter & Object Mouse Exit

Object Mouse Enter uses MonoBehaviour.OnMouseEnter, this function is called when the mouse enters the object collider.
Object Mouse Exit use MonoBehaviour.OnMouseExit, this function is called when the mouse exits the object collider.

Object Mouse Down & Object Mouse Up

Object Mouse Down uses MonoBehaviour.OnMouseDown, this function is called when the user has pressed the left mouse button while over the object collider.
Object Mouse Up uses MonoBehaviour.OnMouseUp, this function is called when the user has released the left mouse button while over the object collider.

UI Mouse Enter & UI Mouse Exit

UI Mouse Enter uses OnPointerEnter inherited from IPointerEnterHandler, this function is called when the mouse overlaps the UI element.
UI Mouse Exit uses OnPointerExit inherited from IPointerExitHandler, this function is called when the mouse exits the UI element.

UI Mouse Down & UI Mouse Up

UI Mouse Down uses OnPointerDown inherited from IPointerDownHandler, this function is called when the user has pressed the left mouse button while over the UI element.
UI Mouse Up uses OnPointerUp inherited from IPointerUpHandler, this function is called when the user has released the left mouse button while over the UI element.