Most of the details in this resource are deprecated. See this updated document for the most current Unity + FMOD integration resources.
With the version 2 Unity integration package, many of the steps to integrate FMOD projects into a Unity game have changed since these lessons were first published. I reccomend Daniel Sykora's Viking Village tutorial to see and hear how to get your FMOD projects to integrate with a game built in Unity. Use these steps to help locate all of the required resources (and get details on a few missed steps in the video tutorials).
It is possible to attach FMOD Events to Unity game objects using Unity's Inspector, but anything that makes use of the Parameter timeline in FMOD will require you to "write" some code. Why "write"? Because it's usually a simple process to copy/paste and change Event and variable names to suit your particular needs. These snippets should provide the code you need to get started with parameter-based sound effects and music designs.
Please copy this code and paste it into the C# files for your game. Names in green are simply placeholders here and should be changed to reflect the unique Event names and Parameters you define in your own project.
Use these lines to play a single FMOD Event with C# scripting in Unity.
// These first three lines usually go inside the class definition—the same place where variables are declared [FMODUnity.EventRef] public string intro = "event:/Intro"; // string reference to the FMOD-authored Event named "Intro"; name will appear in the Unity Inspector FMOD.Studio.EventInstance introEv; // Unity EventInstance name for Intro event that was created in FMOD // These lines should be pasted into the section where you want to cue/play the sound introEv = FMODUnity.RuntimeManager.CreateInstance(intro); // EventInstance is linked to the Event introEv.start (); // FINALLY... Play the sound!!
Use these lines to play a single FMOD Event and control a FMOD-authored parameter using C# in Unity.
Note the different parameter references progress_ge and Progress_MW:
// These first four lines usually go inside the class definition—the same place where variables are declared [FMODUnity.EventRef] public string loop = "event:/Loop"; // string reference to the FMOD-authored Event named "Loop"; name will appear in the Unity Inspector FMOD.Studio.EventInstance loopEv; // Unity EventInstance name for Loop event that was created in FMOD FMOD.Studio.ParameterInstance progress_ge; // Unity ParameterInstance name -- used later to transform Progress_MW (a FMOD parameter within the Loop Event) // These lines should be pasted into the section where you want to cue/play the sound // Note the subtle differences between Progress and progress. The capitalized name is used for the parameter in the FMOD project; the lowercase name is only used in the script loopEv = FMODUnity.RuntimeManager.CreateInstance(loop); // EventInstance is linked to the Event from above (event:/Loop) loopEv.getParameter("Progress_MW", out progress_ge); // Progress parameter authored in FMOD is linked to progress ParameterInstance created above; both associated with the EventInstance loopEv.start (); // FINALLY... Play the sound!! // This line goes wherever you need to update the current value within your game that directs changes to a parameter. // In this example, assume that players are working their way across a terrain. Their progress can be quantified in some way, and that numeric value is used to set the position along the Parameter Timeline in the FMOD Event. // Be sure that the numbers coming from Unity are equivalent to the range set in the FMOD Parameter Timeline progress_ge.setValue(inGameValueToSetPositionAlongFMODParameterTimeline);
FMOD's AHDSR Envelope is one of the best features to create smooth transitions. Rather than stop a sound or music track with an abrupt silence, you can use the AHDSR Envelope to gradually lower the volume over time. While the specifics of the envelope are defined in FMOD, the call to stop the sound using those parameters is done like this with C# in Unity.
// STOP_MODE.ALLOWFADEOUT uses AHDSR modulation envelope on the Event's Master volume knob to set a fade time and shape
// I usually define the AHDSR settings on the Master track of the FMOD Event so as to control all tracks within the Event itself
// This line goes wherever you need to stop/silence a sound effect or music track
loopEv.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT); // "loopEv" is the EventInstance name assigned to the Event
Need to track player input? These statements will check for the first directional key press that initiates game play.
// You have to declare a variable for the state or condition you want to monitor. Put this in the class definition with other variable declarations private bool started = false; // These statements go in a function like FixedUpdate(); where/whenever the game monitors conditions on a frequent and continuous basis if (!started) { Debug.Log("waiting to start..."); // print status to Unity Console if (Input.GetAxis ("Horizontal") != 0 || Input.GetAxis ("Vertical") != 0 ) { // all of these test keyboard for initial player input on any arrow key print ("a directional key was pressed"); // same as Debug.Log to update status yourFunctionToStartSomethingInterestingHere // when input is detected, call the function that starts the main music loop } }