FMOD & Unity Integration 18


Two of our Game prototypes have a gameplay based on Music, Glitchhikers and Tribal Age. So far we had been working using Unity on Tribal and Moai on Glitch (Unreal4 in the near future)

One of the main limitations of using Unity3D , was the technological gap between what we could do on our DAW of preference , and its implementation on the Engine.

Each of the new functionality had to be developed by ourselves, taking into account performance (mobile is our target platform ), and every other aspect of that particular feature. For example, something as simple as the execution of a random sound on an object, would lead us to have all the different audio sources in a single object, with some script to handle randomness, or something as tricky and hacky as merging all the audio in a single file separated by a fixed amount of time, and execute a fraction of that file each time (well, this was just a bad idea, nothing to do with Unity).

But a month ago, Fran mentioned this software, that happens to be a standard in the Games Industry, FMOD. And guess what, the Unity Integration plugin was out of the oven just then. So, at that time we were working on an Interactive Book for a client (we have to make our living!), and decided to give it a try.

The difference was HUGE. You just need to ask Rodrigo (our dedicated Musician). He was always waiting for us (the programmers) to write the new audio functionalities, and then testing on Unity3D (hostile environment for musicians). He would normally just give the separate audio assets to us and then we would set volumes, effects and all that inside Unity.

When he got to work with FMOD, most, if not everything, of those things were on his side. That give us much more control over the process, improved our workflow and introduce new tools.

So, I’m going to walk you trough the process of installation (poor documentation out there) of FMOD on unity. Then I’m going to show you how easy is to randomize audio assets and trigger events.

Installing and setting up FMOD – Unity plugin

First we need to download the unity integration plugin from:

http://www.fmod.org/download/

After that open a Project in Unity, and import the package. Go to Assets -> Import new package -> Custom package and select the plugin file, or just double-clicking the file would do the trick

Now that you have the plugin imported, you have all the folders of the plugin inside your project. If you go to Assets/Plugins/FMOD/ you are going to find the two scripts we are going to use. One is called FMOD_Listener.cs and the other FMOD_StudioEventEmitter

The FMOD_Listener script has to be attached to a GameObject inside the Scene. The most suitable one, would be the MainCamera , given that the listener would be “hearing” the sounds of all the objects on the Scene, and the volume is going to vary according to the distance between the object that has the listener and the objects that are emitting sound.

fmod1

Now we need sounds to reproduce. Download, install and open FMOD Studio

Creating and exporting an FMOD Multi-Sound

Under the events tab right-click the empty space and add a New Event. In FMOD an Event is something that is going to be triggered on the game, it doesn’t need to be a single sound it could be a multi-sound or a set of tracks, it can take parameters that would impact on things like volume, transition and what part of the track you want to loop.

fmod2

Lets create a multi-sound. Select the track called Audio 1 and right-click on the time-line. Select the Add Multi Sound option.

fmod3

Select the Multi Sound and Drag and Drop a couple of sounds to the square that says Drop sounds here. Do you see a yellow and black dice icon? If that is on the multi-sound is randomized, meaning that every time you play that event one of the sounds of the list is going to be reproduce randomly.

fmod4

Ok, now that we have an event with a multi-sound in it, we have to add this Event to a Bank. A bank is a collection of events, and is what we are going to export into Unity. To add an Event to a Bank just drag the Event to Banks Tab and then to the Master Bank.

fmod5

Importing an FMOD bank and Trigger an Event

Now, we are ready to export this little project to Unity, save the project if you didn’t, otherwise you are not going to be able to build. On FMOD Studio go to File -> Build... By default the project created by FMOD has a folder inside called Build where the bank is saved. On Unity go to FMOD -> Import Banks and select the Build folder inside the FMOD project

fmod7

You should see a folder named FMODAssets, containing the events you’ve imported.

After all that is time to hear some sound. Create a Cube, or whatever GameObject you like. Attach the FMOD_StudioEventEmitter script to it, select the GameObject, and look at the inspector tab. The FMOD_StudioEventEmitter has a field named Asset. Click on the circle at the right of that field and the Select FMOD Assets box should pop-up. Select the tab Assets and a list of all the FMOD Events should appear (one in this particular case).

fmod6

If you left the Start on Awake option on, one of the random sounds should start when you hit play. This is OK if you are planing on using the sound as for example Background music, but it would be better to write something to trigger the sound whenever we want. Lets trigger the sound when we click the cube.

The script to achieve that is trivial, take a look:


using UnityEngine;
using System.Collections;

public class PlaySoundOnClick : MonoBehaviour {
    
    FMOD_StudioEventEmitter emitter;
    
    void Awake() {
       emitter = GetComponent<FMOD_StudioEventEmitter>();
    }

    void OnMouseDown() {
        emitter.Play();    
    }
}

You just need to grab the event emitter, and execute the Play() method. If everything went right, you should hear a random sound among the sounds that are inside the multi-sound event each time you click the Cube


Leave a comment

Your email address will not be published. Required fields are marked *

18 thoughts on “FMOD & Unity Integration

  • Michael Klier

    This was the most helpful article I’ve found on this subject. I was banging my head against my desk for almost three weeks to get this working. Thanks to you I finally was able to figure it out! Thanks a lot! (Somehow the lack of official documentation really baffles me though).

    • Guido Villaverde Post author

      Yes! That was why I wrote this. Poor information on the integration. We are using FMOD a lot in our games, so feel free to ask about it! (Sorry for the delay on the response! I was away!)

  • lewis bean

    hey wondering if you had anymore advanced methods on scripting for fmod?

    As im kinda after implementing Fmod functions within other scripts that other members of my team create.

    Lewis

    • Guido Villaverde Post author

      Hey, sorry for the delay, I was away! Yes, I have other interesting stuff, like accessing the C++ API. What kind of methods are you interested in? I could come up with other post about this subject! Cheers!

  • Ryan Murdoch

    First off this is super helpful! Ive been trying to figure this out for a while with little luck!
    I do however have a problem when I import the build from the build folder I get the ‘streaming assets’ folder but I don’t get any of the actual events from FMOD imported in an assets folder like in the pictures above. 🙁
    Any advice? Thanks very much for all your help

  • David

    Hi, thanks for the informative article. I’m currently considering using fmod for a unity project but am confused whether that’s only possible if everyone woking on that project has a unity pro licence. Fmod documentation is a bit unclear on that. I’m the sound guy and I happen to have a pro licence, but none of the other indie team members do. i also read about a workaround for that on another blog, but that sounded a bit tricky. What’s your experience and do you know any workarounds if not everyone has a pro licence?

  • Dale Smith

    I have just began learning FMOD along with Unity as part of my University course and found this very helpful. Is there any chance you could explain how to assign parameters to distance within game objects? I cant find any information regarding this and any help would be appreciated!

    • Ruben

      Hi Dale,

      this works:

      void Update() {

      float distance = Vector3.Distance (this.gameObject.transform.position, cube1.transform.position);
      // “distance” is the name of a value which equals the distance between the objects that this script is attached to and the object which is defined next

      float divideddistance = 1 – Mathf.Clamp (distance / 20f, 0, 1);
      // “divideddistance” is the name of a value which equals the value “distance” defined above gone thru the following equation
      // This is done to create a usable value instead of the units delivered by unity
      // basically this line just says: scale value A to value B

      musicStringsvolume.setValue (divideddistance);
      // This is where you define that the parameter “Stringsvolume” of the event “music” is assigned the same value as “divideddistance” mentioned above.

      }

  • Richard Lucas Ash

    Trying to find which integration package to download that doesn’t give this error:
    Assets/Editor/FMODEventEmitterInspector.cs(5,42); error CS0118: ‘Editor’ is a ‘namespace’ but a ‘type was expected

  • Ruben

    Hi,

    Can you explain, or have come across the following?

    Adaptive Audio is working fine in the unity (free) editor , but when i build mac a standalone (mac and win) there is no sound whatsoever…

    Cheers,

    Ruben

  • Remington Chan

    Hello!

    Wonderful article you have here!

    I’ve tinkered with FMOD for just a little bit but I have a question.

    Do you also experience delays in the audio playing on mobile builds? (for example, simply calling a play one shot on touch input). And if so, how did you overcome this dilemma?

    Thanks!

  • Cole

    Amazing article!

    I am a sound designer and I have gotten decently good at working within FMOD… I am however currently using it on my first long distance project. I need to be able to export the events that I have created to the programmer… I am just not sure what the best way to do all of that is. Is there a specific Package I should export from my Unity project which I am using to implement and get my events to play in-game, or should I really just be sending the FMOD project folder so that they can import the FMOD builds themselves?

    • Guido Villaverde Post author

      Well, it really depends on the workflow they prefer. In our case we have a repo with the FMOD build, and the sound designer has no need to do anything inside Unity. We have custom scripts and behaviors for managing audio. If we were to ask the designer to do something inside Unity it would be using our stuff. At the end, if they are going to use FMOD with unity, they have to do the integration themselves, so they should know how to use the FMOD builds. But if you have some interesting scripts for managing parameters or some added value inside unity, you should mention it and ask if the would like to use those.

  • Alberto Carlassare

    Hi,
    very useful your post. It’s a pity there’s no documentation about FMOD/Unity Integration.
    I’m a Sound Designer getting into Unity coding only to know how programmers should code the events I create in FMOD but I can’t get this stuff working.
    In which script do I write the triggering code for my event?
    If I keep Start on Awake selected the sound plays (so FMOD Studio is working inside Unity). If I unselect Start on Awake how do I tell to Unity to trigger that event? Why don’t use PlayOneShot function?
    Thanks in advance for your help