Main

Playing FMOD Events in Unity (FMOD + Unity Tutorial)

A tutorial on how to play 2D and 3D FMOD Events in Unity using the provided components and writing C# Code. Link to the text tutorial: https://alessandrofama.com/tutorials/fmod-unity/events/ Free-Cam script: https://pastebin.com/FVeLKFDk

Alessandro Famà

4 years ago

In this tutorial, we’ll take a closer look at how to play FMOD events in Unity. The three main ways to play events are the following: 1) Using the Studio Event Emitter component provided by the FMOD integration 2) Using The PlayOneShot helper method in the code 3) Manually starting and stopping FMOD instances in the code - The Event Emitter is great play events without code. In combination with the Studio Parameter Trigger component we can additionally change game parameters. - With PlayOneShot
we can play a sound with just one line of code. FMOD automatically takes care of the playback, stops and releases the sound instance from main memory. A disadvantage of this method is that no parameter changes are possible. If the sound is to be changed by an in-game parameter, then PlayOneShot is not very useful. - The manual playback of events, on the other hand, requires more code, but is much more flexible to manipulate, since you can exactly define when a sound or loop should be played and
stopped, and where parameter changes should happen. Let's go through these methods one by one. In FMOD Studio create a new 2D Event and insert a simple audio file in the timeline. Assign the event to the Master Bank and build the project by pressing F7 or clicking on file->build. Now switch to Unity. Create a new GameObject and insert the Studio Event Emitter Component into the GameObject. Here we find various options that we can change to influence the playback of events. Under Play Event and S
top Event we choose when the event should be played and stopped by using Unity's callback methods. I will choose Object Enable and Object Disable for this tutorial's purpose. In "Event" we select the event from a list of events that we have previously built. Select the event we created, go into Play Mode, activate and deactivate the GameObject to hear the sound. First, we will delete the Start() method, as we don't need it and we will insert our code inside the Update() method instead. We will c
reate an IF-statement and check if the space bar is pressed. If this is the case, we will play back a sound using the PlayOneShot helper method. The PlayOneShot method creates and immediately plays an instance of an FMOD event. The event will play to the end and parameters cannot be set as mentioned before. To use the PlayOneShot method, we have to specify where the event is located and insert the path location as an argument in the method. The path follows the folder structure in the FMOD Studi
o event hierarchy. You can simply right-click on an event in FMOD Studio to copy its path. Return to Unity, insert the script we created into the GameObject and start the scene. By pressing the space bar you will hear the sound Let's start by creating an empty GameObject and a new script like we did in the PlayOneShot section. Give the script your desired name, in my case it's Manual2DExample. Before proceeding, let's summarize the steps needed to manually an event: Four steps are necessary to m
anually play and control an event in Unity: 1. We need to declare an instance. 2. Tell Unity when an instance of the FMOD event should be created and where the event. is located. 3. Start or stop the instance. And as a last step free the instance from memory. Let us now go through these steps one by one. We must declare all events that will be used in this script as an instance before proceeding. In our case, we enter this line at the beginning of the class. We now create our FMOD Event Instance
in Unity's Update method and insert the event path as an argument in the CreateInstance method. We also use an IF-Statement to only create the event when we press the space bar. We start the event by calling the start method of the instance And call the release method of the instance to release it from memory when the sound finishes playing. Go back to Unity, insert the script into the GameObject, start the scene and press the space bar a few times. You will hear the sound playing. An event is
a sound unit that you create in FMOD Studio. It can have various tracks, parameters and automations. A copy or instance of the event is created in-game. You don't start the event itself, but you create an instance from that event. This allows you to have multiple copies of an event playing at the same time. If we put the instance creation code inside Unity's Start method and start the scene again, playing back the sound by pressing the space bar will interrupt other sounds playing, as we just cr
eated one instance at the start of the game. Let's go back to FMOD Studio and create a new event called Loop. I will insert a simple kick loop into the timeline. By right-clicking on the timeline and selecting "Add Loop Region" we can create a loop. Move the loop region to include the whole audio file. Add the event to the master bank and build the project. In Unity create a new GameObject and a new script called Loop. Loops can be played very easily using the manual method. We declare an FMOD E
vent Instance at the start of the class. Then, we create and play the instance inside an If-statement that checks if we are pressing the space bar. This time we will add an additional If statement that checks if pressed the left control key. In that case, we will stop the loop. ALLOWFADEOUT respects the release time in the AHDSR modulation of the event. If we want an abrupt stop, we use STOP_MODE.IMMEDIATE instead. Go back to Unity, insert the script into the GameObject and alternate between pre
ssing the space bar and the left control key. The event will start and stop. Create a new 3D event and insert your desired audio file into the timeline. I will insert a music track for this tutorial. 3D events differ from 2D events in that they play a 3D sound with spatialization The effects adds volume attenuation based on the distance from the instance to the listener, and a panning based on the position of the instance in the direction in which the listener is directed. You can find in-depth
information about the spatializer effect in the FMOD documentation. Add the event to the master bank, build the banks and head over to Unity. Create a new 3D Cube and insert the Studio Event Emitter component into the GameObject. Select the event and choose the Object Enable/Disable callbacks. If you click on "Override Attenuation" you can the spatializer min-max settings for this instance. Insert the FreeCam script provided in the description into the Camera GameObject, start the scene and move
around. You will hear the attenuation and panning taking place. The code to play 3D events manually in Unity is very similar to the 2D events one. Instead of hardcoding the path in the CreateInstance method, we will declare a public string with the EventRef attribute, to be able select our event directly in Unity's inspector this time. We only need to make one minor change, namely to tell FMOD where the instance is located in 3D space. We can do this in Unity's Update method by using the set3DA
ttributes method provided by FMOD. Save the script, create a 3D cube as we did before, insert the script into the GameObject, select your 3D event and start the scene. In alternative to set3DAttributes we can also attach the instance to the GameObject by using the AttachInstanceToGameObject method of FMOD's RuntimeManager. This second method needs to be called one time after creating the instance. It uses the RigidBody to get the velocity of the GameObject and to activate a doppler effect for ex
ample. Congratualation, you now should know how to play FMOD events in Unity. Feel free to write a comment down below if anything is unclear. In the next tutorial we will learn how to control FMOD Parameters in Unity.

Comments

@alessandrofama2095

FYI: The EventRef attribute (9:24) has been removed in the latest version of the Unity integration. Declare an EventReference instead: public FMODUnity.EventReference fmodEvent;

@Etzionid

finally someone that explains it straight and to the point! thank you

@jacksorjacksor

Yeah, definitely my favourite FMOD tutorials that I've found so far. Thank you so much for making them!

@MellioMusic

Your videos are seriously so helpful. Thanks for making these accessible to everyone!

@maestradebobobo767

Thanks man, these tutorials are very helpful

@vano2vano

thanks a lot! compact and clean

@francescosoave1227

Hi! Thanks a lot, By any chance you have experience with multichannel outputs? I did setup 3 events in FMOD, using the Channel Mixer to route them to different analog outs of Motu 828. They work fine in FMOD but when I play the sounds from Unity they play on the Oculus headset speakers rather than the Motu. Any idea what could it be? thanks

@user-ou5qg1kc9x

Огромное спасибо за урок! Вы мне очень помогли ;3

@B4RN154N

Excellent. Thanks!

@DiegoHodgeAudio

I'm curious about the FMOD Studio Event Emitter component. You mentioned that PlayOneShot will automatically stop and release the instance once it's finished playing. If I leave the stop event set to none on the Event Emitter component, will it automatically be stopped and released when finished playing, or is it necessary to manually stop it? What about when the game object that the event emitter is attached to is destroyed? The situation I'm in is my play event is set to on destroyed (realizing maybe on disabled is a better choice) but how would I stop the event (assuming I need to) in that case? In the case that I need to stop it manually, I could add a command instrument after the audio in the event, or I could set and AHDSR with a long enough release and then set stop event to the same as play event and just check allow fadeout, but maybe there's a better option?

@Solcius123123

Thanks a lot !